- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经实现了一个投票系统来检查用户是否可以查看他没有订阅的帖子。我在 Controller 的一个 Action 中调用它。
$this->denyAccessUnlessGranted('view', $post, 'You do not have permission to
view this post!');
如果投票者返回 true,则将其重定向到 twig 模板。
如果相同的模板返回 false 并显示消息“您无权查看此帖子!”,我该如何渲染相同的模板? ?
编辑得更清楚:我不希望用户通过更改 url 中的帖子 ID 来查看他未订阅的帖子。所以,我已经实现了选民来检查这一点。如果 voter 返回 true,则呈现 twig 模板,否则将显示没有模板的消息。我希望在模板中显示此消息。
我想在我的 Twig 模板中使用这样的东西:
{% if is_granted('view', post) %}
post
{% else %}
Permission denied
{% endif %}
最佳答案
对于 future ,请提供更多背景信息。比如你在哪里叫这个?我假设在 Controller 中?
我进一步假设它在 Controller 的操作中。
从您的评论中得出的下一个假设是,如果用户具有访问权限,您希望呈现模板,否则会重定向他。
如果是这种情况,你可以这样做:
public function fooAction()
{
// if it's not in a controller, but you have a container at $container
// you can call $container->get('security.authorization_checker')->isGranted('view', $post);
if (!$this->isGranted('view', $post)) {
return $this->redirect('https://example.com/denied');
// or if you have a route let's call it "app_denied"
//return $this->redirectToRoute('app_denied', ['param' => 'value', 'param2' => 'baz']);
}
// replace `view.html.twig` with your template
return $this->render('view.html.twig', [
'post' => $post,
]);
}
编辑:如果您想抛出异常,请查看自定义错误页面。您可以在 Symfony Documentation 中找到教程。
编辑 2:基于 OP 输入
你应该可以在 twig 中使用 is_granted
。
你可以这样做:
{% if is_granted('view', post) %}
Show the post here
{% else %}
Sorry you don't have permissions to access this!
{% endif %}
您唯一需要注意的是,post
变量已设置。
如果您只想在某人没有访问权限时显示一条消息,您可以使用:
{% if not is_granted('view', post) %}
Sorry you don't have permissions to access this!
{% endif %}
编辑 3:OP 询问如何在 twig 中设置 post
变量。我在这里再次假设,所以你可能有一个 Controller 并使用类似的东西:
return $this->render('view.html.twig', [
'post' => $post,
'foo' => 'bar',
]);
在这种情况下,post
和 foo
作为变量传递给 twig。如果您有多个 Post
条目,假设在 $posts
并使用类似
return $this->render('view.html.twig', [
'posts' => $posts,
]);
在 twig 文件中,您可以使用 for
循环遍历帖子。
{% for post in posts %}
{% if is_granted('view', post) %}
Jup, show the post
{% else %}
Nope, don't show it
{% endif %}
{% else %}
There are no posts
{% endif %}
我建议您阅读 the chapter about Templating
关于php - Twig 上的 Symfony 选民,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49450033/
Twig 使用 {{ }}、{% %}、{# #} 分隔符。 但是如何在 Twig 模板中显示 {{ }} 呢?我不是在谈论 HTML 转义。 我问这个问题是因为我想在我的 Twig 模板中包含一个
为了转换我的法语 html 口音,我尝试通过在 Twig 中执行此操作来更改编码: {{ ddf.description | convert_encoding('UTF-8', 'HTML-ENTIT
我有一个带有输出元素的 .twig 文件的循环。 我需要为每个元素增加一个值。我知道如何在 PHP 中执行此操作,但不清楚如何使用 twig 文档执行此操作。我真的不能在 Controller 中做到
我有一个 xxx.html.twig 文件,它显示一个页面,但是当我想用不同的数据刷新页面并用新数据更新它时,我有一个选择和一个提交按钮。问题是我不知道如何在 Controller 中调用一个 Act
我需要使用 Liquid 来创建模板,但可能只能访问 Twig 模板语言。这两种语言基本上是相同的东西,还是语言上存在差异,导致很难使用 Twig 来替代 Liquid? 最佳答案 Twig 和 Li
number rounding 的 Twig 文档谈论四舍五入小数,但我有一个案例,我想将 19,995 这样的数字四舍五入到 20,000。是否有一种巧妙的方法可以四舍五入到最接近的千位? 最佳答案
我在 2.7 和 3.1 版本中都有 Symfony 项目。 PhpStorm 启用了 Symfony 插件和 Twig 支持插件。 当我在 Symfony 2.7 版本的 PhpStorm 中工作时
我已升级到新版本的 twig/twig 3.x.x。从 twig 版本 3.0 开始,Twig_Extension 已被弃用。所以我接下来要讨论“whiteoctober/breadcrumbs-bu
我在 Twig 中经常有类似的样板代码: {% if (somelongcond) %} {% endif %} Some long content {% if (somelongcond)
我想在我自己的插件中覆盖来自插件的 Twig 模板的 block 。我过去曾覆盖过 native 商店软件模板,但我在使用插件模板时遇到了麻烦。 我正在尝试调整的插件是 FroshProductCom
我用带 Twig 的silex; 我创建自定义 form_div_layout 并将其放在 webroot 中(例如) 我这样注册 TwigService 提供者 $app->register
我不太明白 Twig 中的属性函数是如何工作的。有人可以帮我举个例子吗? 我在 SQL 中有一个名为动态的字段。我可以是例如“field27”,但我不知道号码,号码保存在 radio.id 中。我想做
我想知道是否有一种方法可以在 TWIG 中创建多语句 示例:两个单独的语句... {% set foo:bar %} {% set baz:qux %} 合并成一个语句 {% set foo:
我正在尝试使用 Twig i18n 扩展。 据我所知,我需要的文件在这里: https://github.com/fabpot/Twig-extensions/blob/master/lib/Twig
我正在使用 patternlab 的节点版本用 Twig 作为模板引擎。我正在使用 twig,因为我的代码库是用 twig 编写的 - 所以使用 mustache 不是一个选项。 我简单地尝试包含一个
我有以下代码: {% set pageDescription = item.description|length > 197 ? item.description|striptags|trim|sli
我有以下 Twig 代码: {% for likeditem in user.getItemLikes() %} //iterate over each liked items here {%
反正有没有像这样在 Twig 中重复一个块: {% block title %}{% endblock %} | MyBusiness 为了只声明两个块一次?像那样: {% block title
编辑:2016年12月3日 想学习如何向 Twig 添加自定义扩展(过滤器)吗?请参阅this answer的lxg 您是否只需要找到与ucword等效的嫩枝?请参阅this answer的Javie
我有一个简单的浮点数组。我需要将它显示为逗号分隔的字符串。 {{ arr|join(', ') }} 由于过度的微不足道的准确性,这是一个糟糕的解决方案。 {% for val in arr %}
我是一名优秀的程序员,十分优秀!