如何使用php inline和twig模板?

时间:2017-04-26 14:21:56

标签: php html laravel twig blade

这适用于刀片,但我怎么能在树枝上做到这一点? Twig抛出以下错误:“打开的括号未正确关闭。意外的标记”标点符号“值”:“(”标点符号“预期值为”)“)”

<!DOCTYPE html>
<html>
    <head>
        <meta charset = "utf-8">

        <link rel = "stylesheet" type = "text/css" href = "bootstrap/dist/css/bootstrap.css">
        <link rel = "stylesheet" type = "text/css" href = "bootstrap/dist/css/bootstrap-theme.css">
        <link rel = "stylesheet" type = "text/css" href = "bootstrap/MaterialDesign/css/materialdesignicons.css">

        <link href = "bootstrap/MaterialDesign/fonts/materialdesignicons-webfont.eot">
        <link href = "bootstrap/MaterialDesign/fonts/materialdesignicons-webfont.svg">
        <link href = "bootstrap/MaterialDesign/fonts/materialdesignicons-webfont.ttf">
        <link href = "bootstrap/MaterialDesign/fonts/materialdesignicons-webfont.woff">
        <link href = "bootstrap/MaterialDesign/fonts/materialdesignicons-webfont.woff2">

        <script src = "bootstrap/dist/js/bootstrap.js"></script>
        <script src = "bootstrap/dist/js/npm.js"></script>
        <script src = "react/react.js"></script>
        <script src = "react/react-dom.js"></script>
        <script src = "react/browser.js"></script>

        <style>
            body{
                font-family:Arial;
                font-size: 15px;
            }
        </style>
    </head>

    <body>
        <div class = "container-fluid">
            <div class = "col-sm-2">
                <div class = "well well-sm">TODO list
                    <svg style="width:24px;height:24px; align-content:right" viewBox="0 0 24 24">
                        <path fill="#000000" d="M19,4H15.5L14.5,3H9.5L8.5,4H5V6H19M6,19A2,2 0 0,0 8,21H16A2,2 0 0,0 18,19V7H6V19Z" />
                    </svg>
                </div>

                <div id = "list"></div>

                <svg style="width:24px;height:24px" viewBox="0 0 24 24">
                        <path fill="#000000" d="M19,13H13V19H11V13H5V11H11V5H13V11H19V13Z" />
                </svg><input type = "text"/>     
            </div>
        </div>

        <script type = "text/babel">

            var list = React.createClass ({
                render: function(){
                    return (<div>
                        <svg style="width:20px;height:20px" viewBox="0 0 24 24">
                            <path fill="#000000" d=[M12,20A8,8 0 0,1 4,12A8,8 0 0,1 12,4A8,8 0 0,1 20,12A8,8 0 0,1 12,20M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2Z] />
                        </svg>    
                        <p>{this.props.item}</p>
                    </div>
                    );
                }
            });

            ReactDOM.render(<div>
                                <list item = "Feed the dog" />
                                <list item = "Build the house" />
                                <list item = "Wash the car" />
                                <list item = "Make some shopping" />
                                <list item = "Build a list" />
                            </div>, 
            document.getElementById("list"));
        </script>
    </body>
</html>

5 个答案:

答案 0 :(得分:2)

我自己找到了答案。我正在使用Laravel 5.1,使用rcrowe / TwigBridge。我能够做到这一点:

{{ app.request.is('admin/users') ? 'class=active' }}

答案 1 :(得分:0)

Twig中的if语句与PHP的if语句相当。

在最简单的形式中,您可以将它用于多个分支elseif,否则可以像在PHP中一样使用。你也可以在那里使用更复杂的表达式:

printf( "%.*s", integerValue, charPointer );

如下所述:

  

https://twig.sensiolabs.org/doc/2.x/tags/if.html

但是你的例子还可以,你只需要摆脱额外的括号 例如:

{% if kenny.sick %}
    Kenny is sick.
{% elseif kenny.dead %}
    You killed Kenny! You bastard!!!
{% else %}
    Kenny looks okay --- so far
{% endif %}

答案 2 :(得分:0)

您可以在树枝中访问请求数据。此示例适用于Symfony3中的Twig。

{% if app.request.requestUri == "admin/users" %} class='active'{% endif %}

或作为三元运算符

{{ app.request.requestUri == "admin/users" ? 'class="active"' }}

答案 3 :(得分:0)

额外的括号不是问题。这是一个向你展示的TwigFiddle:

https://twigfiddle.com/623sqv

三元组比使用if语句容易得多,并且您的代码显着简化。您可以在JSON and Twig Ternary Operator上的文章以及Using null-coalescing operator中阅读相关内容。

你只需要总是弄清楚如何正确地进行三元比较。希望这有帮助!

答案 4 :(得分:0)

Twig bridge为laravel的Request外观提供了扩展。它还提供其他扩展。点击此处:https://github.com/rcrowe/TwigBridge#extensions

而不是::,您需要将其与_一起使用,如下所示:

{{ request_is('admin/users') ? 'class=active' }}