简体   繁体   English

如何使用 Twig 从 JSON 中读取数据?

[英]How can i read from JSON with Twig?

I have a simple PHP file:我有一个简单的 PHP 文件:

  <?php

 // Load our autoloader
 require_once __DIR__.'/vendor/autoload.php';

 // Specify our Twig templates location
 $loader = new Twig_Loader_Filesystem(__DIR__.'/views');
 // Instantiate our Twig
 $twig = new Twig_Environment($loader);


 echo $twig->render('index.html', ['products' => $products] );

and now i set the "products" array for the info.现在我为信息设置了“产品”数组。

But i would take variables from a JSON file where i can insert my arrays, someone knows a way?但是我会从 JSON 文件中获取变量,我可以在其中插入我的数组,有人知道方法吗?

Fetch the file contents, json_decode() it into a variable, use it like any other variable:获取文件内容, json_decode() 到一个变量中,像任何其他变量一样使用它:

$json = file_get_contents("test.json");
$products = json_decode($json);

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

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