How to pass arguments to an included file?

前端 未结 9 1932
無奈伤痛
無奈伤痛 2020-12-14 05:53

I\'m trying to make the whole section its own include file. One drawback is the title and description and keyword will be the same; I can\'t figure

9条回答
  •  醉梦人生
    2020-12-14 06:35

    I noticed nobody suggested using a template engine. I came looking here because for the project I'm working with, a template engine isn't possible and that might be your situation too, however I thought it might be worth mentioning these: Twig (my preferred engine) and Smarty both allow passing specific variables to includes.

    I highly recommend the use of a template engine whenever possible, as it simplifies your front end code, adds a layer of abstraction between your front end and back end, and both Twig and Smarty automatically clean the variables you pass to them which helps mitigate XSS attacks.

    Twig Example

    header.html

    
    
    
    
    
    
    
     {{ header }} 
    
    
    
    

    index.html

    {% include 'header.html' with { 'header' : ''} only %}
    Body Text
    {% include 'footer.html' %}
    

    Smarty Example

    header.tpl

    
    
    
    
    
    
    
     {$header} 
    
    
    
    

    index.tpl

    {include 'header.tpl' header=''}
    Body Text
    {include 'footer.tpl'}
    

提交回复
热议问题