symfony入门学习资料之十五:twig模板中的全局变量实现办法

69 篇文章 0 订阅
59 篇文章 6 订阅

symfony入门学习资料之十五:twig模板中的全局变量实现办法

 


     一个网站总有一些网站名等之类的公用信息,这些公用信息体现在它是网站配置信息变量,这一些信息在网站是公用的,所以在TP开发中我们做一引父类的Controller,在其_initialize中实现配置的读取。
       在symfony中,要达到此类结果,用服务注入进行解决。

在MobileBundle中新建一个Service 创建

<?php
namespace Moblie\MobleBundle\Service;
use Doctrine\ORM\EntityManager;
class ConfigServer
{
    protected           $configs;
    protected     $entityManager;
    protected               $cid;

    public function __construct($entityManager)
    {

        $this->entityManager=$entityManager;
        $repository =  $this->entityManager->getRepository('AppBundle:Config');
        $query = $repository->createQueryBuilder('p')->getQuery();
        $configs = $query->getArrayResult();
        $arrCon=null;
        foreach ($configs as $item){
            $arrCon[$item['name']]=$item['value'];
        }
        $this->configs=$arrCon;

    }

    public function getGlobals(){
            return $this->configs;

    }

   

}

$entityManager 为构造function传入的参数,类型是doctrine.orm.entity_manager,这里体现不出来,但在配置里加入以下代码:

    app.configs:
        class: Moblie\MobleBundle\Service\ConfigServer
        arguments: ["@doctrine.orm.entity_manager"]

调用服务的代码我们写在一个基类里:
形式如下:
在基类的twig

{% set confs= configs.getGlobals %}
<!DOCTYPE HTML>
<html>
<head>
    <title>{% block title %}{{ confs.webname }}{% endblock %}</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="keywords" content="" />
    <script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script>
    <link href="{{ asset('css/bootstrap.min.css')}}" rel='stylesheet' type='text/css' />
    <link href="{{ asset('css/style.css')}}" rel='stylesheet' type='text/css' />
    <link href="css/font-awesome.css" rel="stylesheet">
    <script src="{{ asset('js/jquery.min.js')}}"></script>
    <link href='http://fonts.useso.com/css?family=Roboto:400,100,300,500,700,900' rel='stylesheet' type='text/css'>
    <script src="{{ asset('js/bootstrap.min.js')}}"></script>
</head>
<body>
<div id="wrapper">
{{ include('public/top.html.twig') }}
    <div id="page-wrapper">
        <div class="graphs">
            {% block body %}{% endblock %}
{{ include('public/footer.html.twig') }}
        </div>
    </div>
</div>
<link href="{{ asset('css/custom.css')}}" rel="stylesheet">
<script src="{{ asset('js/metisMenu.min.js')}}"></script>
<script src="{{ asset('js/custom.js')}}"></script>
{% block javascripts %}{% endblock %}
</body>
</html>

{% set confs= configs.getGlobals %}  这里引入了服务及服务方法。让方法读出的数组线了confs.
其他页面调用:
{% extends 'base.html.twig' %}
{% block title %}

    {{ confs.webname }}

{% endblock %}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jyl_sh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值