Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render by Vue instead of Jinja

<template id="task-template">     <h1>My Tasks</h1>     <tasks-app></tasks-app>     <ul class="list-group">         <li class="list-group-item" v-for="task in list">             {{task.body|e}}         </li>     </ul> </template> 

This above is my html. I want to render the code by Vue instead.

<script>     Vue.component('tasks-app', {         template: '#tasks-template',         data: function() {             return {                 list: []             }         }         created: function() {             $.getJson('/api/tasks', function(data) {                 this.list = data;             })         }     })     new Vue({         el: 'body',     }); </script> 

The above is my Vue code, and Jinja raise an exception that 'task' is undefined, what I hope for is that the html code rendered by Vue instead of Jinja, I know it could be done in Laravel with this:

"@{{task.body}}" 

Since I am new to Jinja, could anyone help me out?

like image 802
ramsay Avatar asked Dec 11 '16 03:12

ramsay


People also ask

How does Vue render?

Each Vue component implements a render function. Most of the time, the function will be created by the Vue compiler. When you specify a template on your component, the content of this template will be processed by the Vue compiler that will return a render function.

Why do we use Jinja template?

It is a text-based template language and thus can be used to generate any markup as well as source code. The Jinja template engine allows customization of tags, filters, tests, and globals. Also, unlike the Django template engine, Jinja allows the template designer to call functions with arguments on objects.

Is Vue a templating engine?

The templating language is tightly coupled into Vue. js. Like many other templating engines, it uses double curly braces {{ }} to bind data to your template. That means the information in our data variable we pass after our new Vue statement is immediately available inside our application.

Is Vue a templating language?

Vue uses an HTML-based template syntax that allows you to declaratively bind the rendered DOM to the underlying component instance's data.


2 Answers

The other option is to redefine the delimiters used by Vue.js. This is handy if you have a lot of existing template code and you wish to start adding Vue.js functionality to a Flask or Django project.

var app = new Vue({   el: '#app',   data: {     message: 'Hello Vue!'   },   delimiters: ['[[',']]'] }) 

Then in your HTML you can mix your Jinja and Vue.js template tags:

    <div id="app">       {{normaltemplatetag}}       [[ message ]]      </div> 

Not sure when the "delimiters" property was added, but it is in version 2.0.

like image 185
eezis Avatar answered Sep 22 '22 09:09

eezis


You need to define parts of your template as raw so that Jinja escapes that portion instead of trying to fill it up with its own context.

Here is how you need to do it:

<template id="task-template">   <h1>My Tasks</h1>   <tasks-app></tasks-app>   <ul class="list-group">     <li class="list-group-item" v-for="task in list">         {% raw %}{{task.body|e}}{% endraw %}     </li>   </ul> </template> 

Ref: http://jinja.pocoo.org/docs/dev/templates/#escaping

like image 20
Mani Avatar answered Sep 20 '22 09:09

Mani