创建 Twig HTML 布局(母版)的最佳实践 [英] Best practices to create a Twig HTML Layout (Masterpage)

查看:25
本文介绍了创建 Twig HTML 布局(母版)的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发 C#/ASP.net MVC-App 5 年了,我现在正在学习 PHP.

C#中,我可以为每个新站点使用RenderBody,所以新的HTML内容将在RenderBody()中被替换.然后,对于每个站点,我只有一个新的局部视图和一个新的控制器:

 <头><title></title><body>RenderBody()</body></html>

使用 Twig 时,我有一个骨架布局:

<头><title></title>{%block ablock%}{%endblock%}</html>

对于每个新站点,我需要创建一个新的 child.twig 文件并扩展主布局,然后覆盖ablock".通过这样做,我仍然需要一个 PHP 文件(让我们称它们为 index1.phpindex2.php 等),它使用 child.twig 作为参数.最后,我必须为控制器创建 2 个视图(child.twig + index.php)和另外一个 php 文件.所以我的问题是:

使用 Twig 在 MVC 中创建 HTML 母版页的最佳方法是什么?

我找不到任何提及执行此操作的最佳实践的公共项目/教程.

提前致谢.

解决方案

我总是用共享所有页面的内容创建一个通用模板:

<块引用>

generalTemplate.html

<html lang="es"><头><meta charset="utf-8"><title>{{ page_title }}</title><元名称=作者"><link rel="shortcut icon" href="{{project_path}}resources/images/favicon.ico"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><!-- 这里放一般的 CSS 和 JS -->{% block head %} {% endblock %}<身体><标题></标题>{% 块内容 %}{% 端块 %}<页脚></页脚></html>

然后创建继承通用模板的孩子:

<块引用>

oneChild.html

{% 扩展 "generalTemplate.html" %}{% 块头 %}<!-- 特定库 css 和 js -->{% 结束块 %}{% 块内容 %}<!-- 特定的 HTML 内容-->{% 结束块 %}

<块引用>

index.php

require_once 'Twig/Autoloader.php';Twig_Autoloader::register();$loader = new Twig_Loader_Filesystem(path_to_generalTemplatehtml);$twig = new Twig_Environment($loader, array());$template = $twig->loadTemplate($path_to_oneChildhtml);$data = 数组();$data['project_title'] = $project_title;$data['project_path'] = $project_path;echo $template->render($data);

无论如何,关于 Twig 的文档非常详细:http://twig.sensiolabs.org/documentation

I have been developing C#/ASP.net MVC-App for 5 years and I am now learning PHP.

In C#, I can use RenderBody for each new site, so the new HTML content will be replaced in RenderBody(). Then, I have only one new partial view and one new controller for each site:

 <html>
  <head>
    <title></title>
  </head>
  <body>RenderBody()</body>
</html>

When using Twig, I have a skeleton Layout:

<html>
  <head>
    <title></title>
  </head>
  <body>{%block ablock%}{%endblock%}</body>
</html>

For every new site, I need to make a new child.twig file and extend the main layout, then override the 'ablock'. By doing it this way, I still need a PHP file (let's call them index1.php, index2.php, etc) which calls the twig load functions with the child.twig as a parameter. In the end, I have to create 2 views (the child.twig + index.php) and one more php file for the controller. So my question is :

What is the best way to create an HTML-Masterpage in MVC with Twig?

I could not find any public project / tutorial mentioning the best practices to do this.

Thanks in advance.

解决方案

I always create a general template with the content that share all pages:

generalTemplate.html

<!DOCTYPE html>
<html lang="es">
  <head>
    <meta charset="utf-8">
    <title>{{ page_title }}</title>
    <meta name="Author">
    <link rel="shortcut icon" href="{{project_path}}resources/images/favicon.ico">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- Here put the general CSS and JS -->
    {% block head %} {% endblock %}
  </head>
  <body>
    <header>
    </header>
    {% block content %}{% endblock %}
    <footer>
    </footer>
  </body>
</html>

Then creates the children who inherit general template:

oneChild.html

{% extends "generalTemplate.html" %}

{% block head %}
<!-- Specific libraries css and js -->
{% endblock %}

{% block content %}
<!-- Specific HTML content -->
{% endblock %}

index.php

require_once 'Twig/Autoloader.php'; 

Twig_Autoloader::register();

$loader = new Twig_Loader_Filesystem(path_to_generalTemplatehtml);
$twig = new Twig_Environment($loader, array());

$template = $twig->loadTemplate($path_to_oneChildhtml);

$data = array();
$data['project_title'] = $project_title;
$data['project_path'] = $project_path;

echo $template->render($data);

Anyway, there is a fine documentation on Twig with great detail: http://twig.sensiolabs.org/documentation

这篇关于创建 Twig HTML 布局(母版)的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆