Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Assemble从JSON文件生成多个页面_Javascript_Json_Gruntjs_Assemble - Fatal编程技术网

Javascript 如何使用Assemble从JSON文件生成多个页面

Javascript 如何使用Assemble从JSON文件生成多个页面,javascript,json,gruntjs,assemble,Javascript,Json,Gruntjs,Assemble,所以我正在为可复制的文章构建一个框架 每篇文章将由“部分”组成,这些部分基本上构成一个html块和一些相关内容。例如,一个给定的物品可以组成如下: author section text section image section text section blockquote section carousel section text section 我想象每个部分都有一个部分模板,如前所述,这些部分都有相关数据,因此对于文本部分,它可能只是副本(尽管它可能是一个标记文件?),对于图像部分,

所以我正在为可复制的文章构建一个框架

每篇文章将由“部分”组成,这些部分基本上构成一个html块和一些相关内容。例如,一个给定的物品可以组成如下:

author section
text section
image section
text section
blockquote section
carousel section
text section
我想象每个部分都有一个部分模板,如前所述,这些部分都有相关数据,因此对于文本部分,它可能只是副本(尽管它可能是一个标记文件?),对于图像部分,它可能是URL和alt标记的集合,等等。每篇文章还需要一个相关的CSS&JS文件来进行任何定制的修改,以及它自己的图像回购

我需要在建成后最终得到的最终结构如下所示:

site/
    [shared]css/
    [shared]img/
    [shared]js/
    articles/
        article-01/
            [article specific]css/
            [article specific]img/
            [article specific]js/
            index.html
        article-02/
            [article specific]css/
            [article specific]img/
            [article specific]js/
            index.html
{ 
  "title"   : "Article Title",
  "sections": [
    { 
      "type"  :  "text",
      "data"  : {
        "content"  :  "Lorem iplsum dolor..."
      }
    },
    { 
      "type"  :  "author",
      "data"  : {
        "name"  :  "Bob Servant",
        "meta"  :  "As if you don't know who Bob Servant is",
        "url"  :  "bobservant.com"
      }
    },
    { 
      "type"  :  "image",
      "data"  : {
        "src"   :  "http://placehold.it/300x300",
        "alt" :  "Bob Servant"
      }
    }
  ]
}
显然,复制和粘贴HTML/CSS文件、更改一些值并使用grunt构建这些文件是相当简单的,但我正在尝试避免复制粘贴复制,而assemble似乎就是答案

因此,我的主要计划是使用Assembly来撰写文章,目的是通过填充.json文件或类似文件来生成一篇新文章

在我的脑海里,它看起来像这样:

site/
    [shared]css/
    [shared]img/
    [shared]js/
    articles/
        article-01/
            [article specific]css/
            [article specific]img/
            [article specific]js/
            index.html
        article-02/
            [article specific]css/
            [article specific]img/
            [article specific]js/
            index.html
{ 
  "title"   : "Article Title",
  "sections": [
    { 
      "type"  :  "text",
      "data"  : {
        "content"  :  "Lorem iplsum dolor..."
      }
    },
    { 
      "type"  :  "author",
      "data"  : {
        "name"  :  "Bob Servant",
        "meta"  :  "As if you don't know who Bob Servant is",
        "url"  :  "bobservant.com"
      }
    },
    { 
      "type"  :  "image",
      "data"  : {
        "src"   :  "http://placehold.it/300x300",
        "alt" :  "Bob Servant"
      }
    }
  ]
}
这个.json文件将规定各部分的顺序,并提供内容,希望能够消除对每篇文章使用单独的.hbs模板的需求

在我的默认模板中,我基本上希望实现以下伪代码:

foreach section {
  get the the appropriate template and pass it the associated data object
}
我正在努力解决的另一个问题是,我希望有通用的部分,如text.hbs、image.hbs author.hbs等,但我需要能够在给定的文章中包含多个实例,每个实例都有自己的数据

我想最终这是一个抽象的问题,无论是在模板和数据的解耦程度方面,还是在如何将数据绑定到模板的正确实例方面。。。如果这有道理的话


如果任何人有任何有用的建议,我们将不胜感激。我觉得我要做的事情应该是可以实现的,但我不太清楚如何通过学习示例来实现:|

你可以在
汇编.options
上的
页面
集合中添加页面。有关示例,请参见

您可以创建上面提到的每个部分,这些部分包含
文本
作者
图像
块引号
等所需的数据。。。然后使用从页面元数据动态调用部分

{{#each page.sections}}
  {{partial this.type this.data}}
{{/each}}
试着用这些信息建立一个回购协议,并将其链接到这里,我可以看看它是否按预期工作


希望这有帮助。

您的帖子没有具体问题。你问“我想做x和y,我怎么能做x和y”。你已经知道答案了。