[英]Problems with Twig and PHP
我正在尝试使用Twig和PHP创建一个内容管理系统,但是当我打开页面时,它说那里没有条目。 我已经使用var_dump()
验证了这一点。
路线代码:
$app->get('/articles', function() use ($app) {
$approved_articles = $db_bsa->query("
SELECT id, label, slug
FROM approved_articles
")->fetchAll(PDO::FETCH_ASSOC);
var_dump($approved_articles);
$app->render('main/articles.php');
})->name('articles');
视图的代码:
{% extends 'templates/default.php' %}
{% block title %}Articles{% endblock %}
{% block content %}
<article class="article">
<header>
<h2>Articles</h2>
</header>
<footer>
<p class="post-info">A Directory Style Listing of our Active Articles</p>
</footer>
{% if approved_articles %}
<ul>
{% for approved_article in approved_articles %}
<li>{{ approved_article.label }}</li>
{% endfor %}
</ul>
{% else %}
<p>No articles at the moment...</p>
{% endif %}
</article>
{% endblock %}
另外,此处发布了具有更多背景信息的更长链: https : //www.codecourse.com/forum/topics/problems-using-twig-and-php/329
谢谢您的帮助!
你需要传递approved_articles
到树枝,当你呈现模板。
$app->render('main/articles.php', array('approved_articles' => $approved_articles));
Twig不会在控制器中自动找到变量。 你需要明确传递模板变量,如approved_articles
成嫩枝渲染引擎。
附带说明一下,您的articles.php
模板文件以.php
扩展名命名,但实际上不是PHP文件。 通常,模板以.twig
扩展名命名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.