• Stars
    star
    605
  • Rank 74,072 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Vue component for compiling templates on the fly using a v-html like API

v-runtime-template

npm npm Donate

A Vue.js components that makes easy compiling and interpreting a Vue.js template at runtime by using a v-html like API.

Do you know VueDose? It's where you can learn tips about the Vue.js ecosystem in a concise format, perfect for busy devs! 🦄

See Demo on CodeSandbox

Motivation

This library solves the case where you get a vue-syntax template string on runtime, usually from a server. Think of a feature where you allow the user to create their own interfaces and structures. You save that as a vue template in your database, which your UI will request later. While components are pre-compiled at build time, this case isn't (since the template is received at runtime) and needs to be compiled at runtime.

v-runtime-template compiles that template and attaches it to the scope of the component that uses it, so it has access to its data, props, methods and computed properties.

Think of it as the v-html equivalent that also understands vue template syntax (while v-html is just for plain HTML).

Getting Started

Install it:

npm install v-runtime-template

You must use the with-compiler Vue.js version. This is needed in order to compile on-the-fly Vue.js templates. For that, you can set a webpack alias for vue to the vue/dist/vue.common file.

For example, if you use the Vue CLI, create or modify the vue.config.js file adding the following alias:

// vue.config.js
module.exports = {
  runtimeCompiler: true
};

And in Nuxt, open the nuxt.config.js file and extend the webpack config by adding the following line to the extend key:

// nuxt.config.js
{
  build: {
    extend(config, { isDev, isClient }) {
      config.resolve.alias["vue"] = "vue/dist/vue.common";
      // ...

Usage

You just need to import the v-runtime-template component, and pass the template you want:

<template>
	<div>
		<v-runtime-template :template="template"></v-runtime-template>
	</div>
</template>

<script>
import VRuntimeTemplate from "v-runtime-template";
import AppMessage from "./AppMessage";

export default {
  data: () => ({
    name: "Mellow",
    template: `
      <app-message>Hello {{ name }}!</app-message>
    `
  }),
  components: {
    AppMessage,
    VRuntimeTemplate
  }
};
</script>

The template you pass have access to the parent component instance. For example, in the last example we're using the AppMessage component and accessing the {{ name }} state variable.

But you can access computed properties and methods as well from the template:

export default {
  data: () => ({
    name: "Mellow",
    template: `
      <div>
        <app-message>Hello {{ name }}!</app-message>
        <button @click="sayHi">Say Hi!</button>
        <p>{{ someComputed }}</p>
      </div>
		`,
  }),
  computed: {
    someComputed() {
      return "Wow, I'm computed";
    },
  },
  methods: {
    sayHi() {
      console.log("Hi");
    },
  },
};

Limitations

Keep in mind that the template can only access the instance properties of the component who is using it. Read this issue for more information.

Comparison

v-runtime-template VS v-html

TL;DR: If you need to interpret only HTML, use v-html. Use this library otherwise.

They both have the same goal: to interpret and attach a piece of structure to a scope at runtime. The difference is, [v-html](https://vuejs.org/v2/api/#v-html) doesn't understand vue template syntax, but only HTML. So, while this code works:

<template>
	<div v-html="template"></div>
</template>

<script>
export default {
  data: () => ({
    template: `
      <a href="/mike-page">Go to Mike page</a>
    `

the following wouldn't since it uses the custom router-link component:

<router-link to="mike-page">Go to Mike page</router-link>

But you can use v-runtime-template, which uses basically the same API than v-html:

<template>
	<v-runtime-template :template="template"></v-runtime-template>
</template>

<script>
export default {
  data: () => ({
    template: `
      <router-link to="mike-page">Go to Mike page</router-link>
    `

v-runtime-template VS dynamic components (<component>)

Dynamic components have somewhat different goal: to render a component dynamically by binding it to the is prop. Although, these components are usually pre-compiled. However, the goal of v-runtime-template can be achieved just by using the component options object form of dynamic components.

In fact, v-runtime-template uses that under the hood (in the render function form) along with other common tasks to achieve its goal.

More Repositories

1

typescript-library-starter

Starter kit with zero-config for building a library in TypeScript, featuring RollupJS, Jest, Prettier, TSLint, Semantic Release, and more!
TypeScript
4,348
star
2

v-lazy-image

Lazy load images using Intersection Observer, apply progressive rendering and css animations.
JavaScript
978
star
3

vue-testing-series

JavaScript
158
star
4

Vue-Typescript-Starter

TypeScript starter project based on Vue-cli webpack template
JavaScript
62
star
5

Egghead-Vuex-TypeScript

JavaScript
48
star
6

Egghead-Typescript-Vuejs-apps

JavaScript
42
star
7

vue-pwa

Vue
20
star
8

narutodose

Vue
17
star
9

vue-styleguidist-example

Example on how to use vue-styleguidist with minimal setup
Vue
14
star
10

alexjover.com

Vue
12
star
11

interviews

Code challenges for interviews I made
JavaScript
11
star
12

Faelo-food-order-app-MEAN.js-

Full-Stack food order system made in MEAN.js
JavaScript
10
star
13

storyblok-nuxt3

Vue
10
star
14

react-native-gps-logger

GPS Logger that runs in background using React Native
Java
9
star
15

links

Links and Resources to keep organized my personal interests
7
star
16

nuxt3-storyblok-ecommerce

JavaScript
6
star
17

blog

My personal blog (src)
CSS
6
star
18

Egghead-Vue-Async-Components

Example that shows how to create async components in Vue
JavaScript
5
star
19

reactive-grocery-app-slides

https://alexjoverm.github.io/reactive-grocery-app-slides/
HTML
5
star
20

javascript-algorithms-es6

A set of algorithms written in ES6+
JavaScript
4
star
21

testing-vue-book-ru

4
star
22

MyWeather

Winner multidevice app made in FireMonkey for the 1st app challenge of Embarcadero Technologies
Pascal
4
star
23

egghead-lessons

JavaScript
4
star
24

MP3PlayerJS

Web-based MP3 player made using MVC pattern in vanilla js (plain javascript)
JavaScript
3
star
25

reactive-grocery-app

TypeScript
3
star
26

angular-bulma

Angular 2 UI component library on top of Bulma css framework
TypeScript
3
star
27

frontend-libs-medley

Vite best-practise example on creating a frontend library for Vue, React and Svelte
JavaScript
3
star
28

GaTe

Strategy and platform 2.5D videogame made in C++ and SFML
C++
2
star
29

testing-vue-book-es

2
star
30

Footle

Reservation system for busy people
TypeScript
2
star
31

TreeXplorer

Web-based file explorer made in plain Javascript and. Uses MVC, Browserify and CommonJS for structure.
JavaScript
2
star
32

gridsome-blog-example

An example on using Gridsome to create a blog
Vue
2
star
33

strapi-nuxt

JavaScript
1
star
34

awesome-resources

Personal list of curated resources
1
star
35

vue-simple-starter

Simple Vue.js starter using Poi
JavaScript
1
star
36

RelaxCoach

Simple project which is a prototype for a breathing techniques & heart-rate measurement applications, made with AngularJS
CSS
1
star
37

frontcast

A FrontEnd screencast platform
Vue
1
star
38

pruebasph1

Repositorio para pruebas de ph1
JavaScript
1
star
39

IRC-nw.js-chat

IRC chat made in nw.js (node.js based platform that creates cross-platform desktop apps)
CSS
1
star
40

Egghead_lesson-TypeScript_library_starter

Code for the typescript library starter lessons
JavaScript
1
star
41

Footle_deprecated

Reservation system for busy people
JavaScript
1
star
42

ClockMVCBrowserify

Simple Clock MVC project that uses Browserify to organize javacript files
JavaScript
1
star
43

MyWeb

Repo for my Website.
CSS
1
star
44

GCB-creator-lujan

Versión que añade otro tipo de actividad basada en iframes
JavaScript
1
star
45

Portfolio-client

TypeScript
1
star
46

WebBooks-project-Multimedia-Engineering

WebBooks application made for HyperMedia Development on Multimedia Engineering Degree
CSS
1
star
47

ObserverJS

Simple Observer pattern implementation to use it in hierarchy made in plain javascript
JavaScript
1
star
48

RxJS-FRP-talk

Slides of my talk "RxJS and FRP"
CSS
1
star
49

unnamed-server

TypeScript
1
star
50

DBCityFinder

Web-based project that uses DBpedia and SparQL in order to search and filter cities
JavaScript
1
star
51

Fullstack-scaffolding

Generated scaffolding project
JavaScript
1
star
52

AvadaLESS

Light framework and utils library made on LESS
CSS
1
star
53

TripMinder

Repositorio oficial para el TFG TripMinder
JavaScript
1
star
54

APitems

Analytics and charts webapp made for 2nd LOL API Challenge
JavaScript
1
star