简体   繁体   English

使用javascript渲染树枝模板

[英]Render twig template using javascript

I'm trying to get a twig template to render in javascript with no luck at all. 我正在尝试获取一个树枝模板,以便在javascript中完全没有运气。 The console is not getting me any errors and the template not getting rendered. 控制台没有提示我任何错误,模板也没有呈现。

What am i doing wrong? 我究竟做错了什么?

Code: 码:

import Twig from 'twig';

fetch(videos).then(res => res.json())
  .then(data => {
    data.items.forEach(item => {
        Twig.renderFile('./themes/mytheme/views/components/thumbnail.twig', {item:'item'}, (err, html) => {
            html; 
        });
  });

I am not so sure where do you using Twig (backend or frontend) but I think your problem is you are trying to compile twig file and put the compiled result into some element. 我不确定在哪里使用Twig (后端或前端),但是我认为您的问题是您正在尝试编译twig file并将编译后的结果放入某个元素中。 I try using twig myself before answer you, it seems like Twig.rendering is not method to render. 我尝试用twig答案之前你自己,好像Twig.rendering不是方法来呈现。 It is template file compiling method. 它是模板文件的编译方法。 So, html inside the renderFile s scope is the compiled result. 因此, renderFile范围内的html是已编译的结果。 You have to put it into element somehow. 您必须以某种方式将其放入元素中。 Because of html is string, html; 因为html是字符串,所以html; will do nothing. 什么也不会做。

Actually, you can use console.log(html) to see the result of it. 实际上,您可以使用console.log(html)查看其结果。 (It will be html string.). (它将是html字符串。)。

Just add the method to use html into the scope. 只需将使用html的方法添加到范围中即可。 Then, it should works. 然后,它应该工作。

You need something like this inside your renderFile 您的renderFile需要这样的东西

document.getElementById('container').innerHTML = html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM