[英]How do we use “lookup” in Nodejs to include partials?
We use this syntax to include partial during run time: 我们使用以下语法在运行时包括部分内容:
{{> (lookup . 'file') }}
file is a var name from the parent file. file是父文件的var名称。
I tried to add a prefix to the file name, So I tried: 我试图为文件名添加前缀,所以我尝试:
{{> lookup . 'path/file'}}
{{> (lookup . (strmerge 'path/' 'file')) }}
Note: I made a helper method to merge strings {{> (lookup . (strmerge 'path/' 'file')) }}
注意:我做了一个辅助方法来合并字符串 I tried those and others but nothing worked for me. 我尝试了这些和其他方法,但对我没有任何帮助。
Does any one know how to do this? 有谁知道如何做到这一点?
Thanks 谢谢
In the code {{> (lookup . 'file') }}
we are telling Handlebars that the name of our partial is to be found at the file
property of the current context object. 在代码
{{> (lookup . 'file') }}
我们告诉Handlebars我们的部分的名称可以在当前上下文对象的file
属性中找到。
Assuming a context object like { file: 'myPartial' }
, the result of the lookup is {{> myPartial }}
, which tells Handlebars to render a partial called "myPartial". 假设有一个
{ file: 'myPartial' }
类的上下文对象,查找的结果是{{> myPartial }}
,它告诉Handlebars渲染一个名为“ myPartial”的部分。
If we want to add a prefix to our partial, so that Handlebars will register a partial called "path/myPartial", the simplest way to do this would be to add that path to the value of the file
property in the context object. 如果我们要在部分文件中添加前缀,以使Handlebars注册一个名为“ path / myPartial”的部分,最简单的方法是将该路径添加到上下文对象中
file
属性的值。 The context object would become: { file: 'path/myPartial' }
. 上下文对象将变为:
{ file: 'path/myPartial' }
。
If, for some reason, the "path/" prefix must be added to the template and not the data , then we will need to determine a way to produce the String "path/myPartial" from our current data. 如果由于某种原因必须在模板中添加“ path /”前缀而不是数据 ,那么我们将需要确定一种从当前数据中生成字符串“ path / myPartial”的方法。
Both of your attempts put "file" in the name of the property to be looked-up. 您的两次尝试都将“ file”放在要查找的属性的名称中。 Your code will try to find the property
path/file
on the context object and this will fail. 您的代码将尝试在上下文对象上找到属性
path/file
,这将失败。 We will definitely need a helper to concatenate Strings, but it must concatenate "path/" with the value of file
, not the literal String, "file". 我们肯定需要一个帮助程序来连接字符串,但是它必须将“ path /”与
file
的值(而不是文字字符串“ file”)进行连接。
To achieve our goal we will no longer require the lookup
helper. 为了实现我们的目标,我们将不再需要
lookup
助手。 The lookup
was needed only because you can't write {{> (file) }}
in Handlebars, because Handlebars will treat file
as a helper instead of as a variable. 仅由于您无法在Handlebars中编写
{{> (file) }}
,才需要lookup
,因为Handlebars会将file
视为帮助程序,而不是变量。 However, since we are using a concatenation helper, strmerge
, we can use the String it returns as our partial name, without any need for a lookup
. 但是,由于我们使用串联帮助器
strmerge
,因此可以使用它返回的String作为我们的部分名称,而无需进行lookup
。 The correct code becomes: 正确的代码变为:
{{> (strmerge 'path/' file) }}
It's important to note that file
in this example is not in quotes. 重要的是要注意此示例中的
file
没有用引号引起来。 It is a variable, not a String. 它是一个变量,而不是字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.