[英]Access json element from javascript
I have this JSON, which uses the i18next for i18n a website which uses blade as the templating engine : 我有这个JSON,它将i18next用于i18n一个使用Blade作为模板引擎的网站:
"guide": {
"sections": {
"the-basics": {
"title": "The Basics",
"contents": [
{
"title": "Introduction",
"content": {"p1": "", "p2": "", "p3": ""}
},
{
"title": "A Chapter in the Zeitgeist Movement",
"content": {"p1": "", "p2": "", "p3": ""}
}
]
},
"setting-up-a-national-chapter": {
"title": "Setting up a National Chapter",
"contents": [
{
"title": "Gathering Volunteers & Social Media",
"content": {"p1": "", "p2": "", "p3": ""}
}
]
}
}
}
In my javascript, I am trying to access the contents list, depending on the index number. 在我的JavaScript中,我尝试访问目录列表,具体取决于索引号。
This is what I have so far: 这是我到目前为止的内容:
// Load page
$('a.nav-link').on('click', function( event ) {
event.preventDefault();
var $this = $(this);
var section = $this.closest('ul').prev('h3').find('span[data-content]').data('content');
var subSection = $this.attr("data-content");
// we can now genrate the section content and push this into the tmpl.
blade.Runtime.loadTemplate("guide_section.blade", function(err, tmpl) {
tmpl({
'sections': {
'section': section,
'subsection': subSection
}
}, function(err, html) {
if(err) throw err;
console.log(html);
$('.mainarticle > .content').empty().html(html);
});
});
});
and in my template: 在我的模板中:
#guide_section
- var i = sections.section
- var c = sections.subsection
h5(data-i18n="guide.sections."+i+".title")=i
h4(data-i18n="guide.sections."+i+".contents.title")=c
The above code is very similar to jade's template. 上面的代码与jade的模板非常相似。
The H5
works fine, in that it returns the title, but I am not sure how to return the n-th element for the contents lists. H5
可以正常工作,因为它可以返回标题,但是我不确定如何返回目录列表的第n个元素。
For example: 例如:
h5(data-i18n="guide.sections."+i+".title")=i
renders as 呈现为
<h5 data-i18n="guide.sections.the-basics.title">the-basics</h5
which is correct, as then i18next looks up the translation.json
and sets the correct translation based on the data-i18n
value, but 这是正确的,因为随后i18next会查找translation.json
并根据data-i18n
值设置正确的翻译,但是
data-i18n="guide.sections."+i+".contents.title"
renders as 呈现为
<h4 data-i18n="guide.sections.the-basics.contents[0]">0</h4
but contents
in this case has two entries such as: 但是在这种情况下, contents
有两个条目,例如:
[{
"title": "Introduction",
"content": {"p1": "", "p2": "", "p3": ""}
},{
"title": "A Chapter in the Zeitgeist Movement",
"content": {"p1": "", "p2": "", "p3": ""}
}]
So how do I change my script so that by giving the index, I can access the title of the contents
list? 那么,如何更改脚本,以便通过提供索引来访问contents
列表的标题?
this works, taken from http://i18next.com/pages/doc_features.html#objecttree , so in my case 'content' is an array 这是有效的,取自http://i18next.com/pages/doc_features.html#objecttree ,所以在我的情况下,“内容”是一个数组
ul.sub-section
- var content = t("guide.sections."+i+".contents", { returnObjectTrees: true });
- for(var c in content)
- var section_title = t(content[c].title)
li
a.nav-link(href="#" data-bind=""+c data-content=""+section_title data-i18n="guide.sections."+i+".contents.title")=t(content[c].title)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.