簡體   English   中英

在Module.php中訪問部分

[英]Access partial in Module.php

我有兩個模塊。

  1. 應用
  2. 用戶

布局是在“應用程序”模塊中設置的,在布局中,我需要顯示特定於模塊的導航元素,這是我要嘗試的操作。

這是我在應用程序模塊的布局/布局中調用的部分內容。

//file: module/Application/view/layout/container/layout.phtml
<?php echo $this->partial('toolbar'); ?>

這是我的toolbar.phtml

//file: module/Application/view/layout/chunks/toolbar.phtml
<div class="new-top-menu">
    <div class="navbar-header">
        <a class="navbar-brand sidebar-toggle-box" href="#">
            <div class="sidebar-left-trigger">
                <span class="fa fa-align-right"></span>
            </div>
        </a>
    </div>
    <ul class="nav navbar-nav">
        <?php echo $this->placeholder('notification/top-menu'); ?>
    </ul>
    <ul class="nav navbar-nav navbar-right">
        <?php echo $this->placeholder('toolbar/search'); ?>
        <?php echo $this->placeholder('toolbar/right-position1')->set($this->position1); ?>
        <?php echo $this->placeholder('toolbar/right-position2')->set($this->position2); ?>
        <?php echo $this->placeholder('toolbar/right-position3')->set($this->position3); ?>
    </ul>
</div>

基本上,我想從用戶模塊中讀取另一個名為toolbar/user-menu.phtml部分文件,並將其設置為該占位符的內容$this->placeholder('toolbar/right-position3');

我嘗試在用戶模塊的Module.php中執行此操作。

namespace User;

public function onBootstrap(MvcEvent $e)
{
    $viewModel = $e->getViewModel();
    $viewModel->setVariable('position1', 'test string');
}

這絕對正常,問題是我想用位於file: module/User/view/toolbar/user-menu.phtml partial內容替換內容“ test string”

任何有關如何在ZF2中完成此操作的指針均應受到贊賞。

謝謝。

UPDATE1:

我能夠使用下面的代碼來做到這一點,我期待着無論如何都可以得到改善。

在User / Module.php中,我更新了以下內容。

use Zend\View\Renderer\PhpRenderer;
use Zend\View\Resolver;

public function onBootstrap(MvcEvent $event)
{
    $application = $event->getApplication();
    $eventManager = $application->getEventManager();
    $serviceManager = $application->getServiceManager();
    //create a Template Stack
    $stack = new Resolver\TemplatePathStack(array('script_paths' => array(__DIR__ . '/view/user/toolbar')));

    //create a Resolver
    $resolver = new Resolver\AggregateResolver();
    $resolver->attach($stack);

    //create a Renderer
    $renderer = new PhpRenderer();
    $renderer->setResolver($resolver);

    $viewHelperManager = $serviceManager->get('ViewHelperManager');
    $urlViewHelper = $viewHelperManager->get('url');

    $partial = $renderer->partial('user-menu', array(
        'signOutUrl' => $urlViewHelper('user-account-sign-out')
    ));
    $viewModel = $event->getViewModel();
    $viewModel->setVariable('position1', $partial);
}

就是這樣,我現在能夠將HTML內容放置在占位符中,我現在面臨的唯一問題是訪問user-menu.phtml文件中的視圖助手,我目前正在Module.php中生成它並將其傳遞給視圖文件。

我認為您走在正確的道路上,因為您開始查看本機視圖幫助器,但是您不應該直接在Module中操縱這些幫助器,也不應嘗試更改視圖腳本中的工具欄。

取而代之的是,制作自己的工具欄視圖助手,通過其工廠(包括部分模板)注入依賴項,然后可以在布局視圖腳本中簡單地echo $this->toolbar()

另外,您可能會看看Spiffy導航(如果沒有其他內容),只是關於視圖助手的實現方式。 https://github.com/spiffyjr/spiffy-navigation

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM