Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global variable available everywhere #72

Closed
DPlachkov opened this issue Apr 23, 2019 · 5 comments
Closed

Global variable available everywhere #72

DPlachkov opened this issue Apr 23, 2019 · 5 comments

Comments

@DPlachkov
Copy link

Hello, I'm trying have a variable available everywhere, in Laravel you'd go about this by using
View::share( 'success', $success );
View::share( 'errors', $errors );

Is there a way to implement this in blade, as i am trying to set my flash messages in the middleware/default controller (it is an old framework) and unset them from the session. I need this to be done once every time the page loads, hence why it is not ok to pass them with
$blade->run($path, $variables); everytime in every controller.

@jorgecc
Copy link
Member

jorgecc commented Apr 23, 2019

Ok, let's me see it.

@jorgecc
Copy link
Member

jorgecc commented Apr 23, 2019

Added on 3.23

@jorgecc jorgecc closed this as completed Apr 23, 2019
@askme-gpt
Copy link

@jorgecc how to use share global variables in blade one ? I am using yaf framework

@jorgecc
Copy link
Member

jorgecc commented May 10, 2020

Hi there:

Did you know that you could open a new issue?. It is for free ;-)

Anyways, here it goes:

https://github.com/EFTEC/BladeOne/wiki/Using--BladeOne-with-YAF

@askme-gpt
Copy link

askme-gpt commented May 10, 2020

@jorgecc yes ,i know ,thank you very much . I found a solution before you publish the YAF wiki , and I make yaf templete just like laravel. here is my code:

D:\phpStudy\PHPTutorial\WWW\yaf_test\application\library\BladeCache.php

<?php
use eftec\bladeone\BladeOne;
use eftec\bladeone\BladeOneCache;

class BladeCache extends BladeOne
{
    use BladeOneCache;
}

D:\phpStudy\PHPTutorial\WWW\yaf_test\application\library\BladeAdapter.php

<?php

use eftec\bladeone\BladeOne;

class BladeAdapter implements Yaf\View_Interface
{

    public $_blade;

    /**
     * Constructor
     *
     * @param string $tmplPath
     * @param array $extraParams
     * @return void
     */
    public function __construct()
    {
        $bladeConfig = Yaf\Registry::get("config")->get("blade")->toarray();
        $views       = $bladeConfig['template_dir'] ?? '';
        $cache       = $bladeConfig['cache_dir'] ?? '';
        // 使用缓存
        $this->_blade = new BladeCache($views, $cache);

        // 不使用缓存
        // $this->_blade = new BladeOne($views, $cache, BladeOne::MODE_AUTO);
        $this->_blade->setInjectResolver(function ($className) {
            // Custom logic for resolving
            return new $className();
        });

        // 全局变量
        // $this->_blade->share('lang', 'test');

        foreach ($bladeConfig as $key => $value) {
            $this->_blade->$key = $value;
        }
    }

    /**
     * Set the path to the templates
     *
     * @param string $path The directory to set as the path.
     * @return void
     */
    public function setScriptPath($path)
    {
        if (is_readable($path)) {
            $this->_blade->template_dir = $path;
            return;
        }

        throw new Exception('Invalid path provided');
    }
    /**
     * Retrieve the current template directory
     *
     * @return string
     */
    public function getScriptPath($request = null)
    {
        return $this->_blade->template_dir ?? null;
    }

    public function assign($name, $value = null)
    {
        return true;
    }

    public function render($view_path, $tpl_vars = null)
    {
        return true;
    }

    public function display($view_path, $tpl_vars = null)
    {
        echo $this->_blade->run($view_path, $tpl_vars);
    }

    public function getView()
    {
        return $this->_blade;
    }
}

common helper function:

function view($view_path, $tpl_vars = [])
{
    $blade = new BladeAdapter();
    return $blade->display($view_path, $tpl_vars);
}

then i use blade one in my app like below:

    public function readAction()
    {
        $id   = $this->getRequest()->getQuery('id');
        $data = $this->article->articleInfo($id);
        return view('article.read', $data);
    }

application.ini

[common]
application.directory = APPLICATION_PATH  "/application"
application.dispatcher.catchException = TRUE
application.view.ext=blade.php

[product : common]
blade.template_dir     = APPLICATION_PATH "/application/views/"
blade.cache_dir        = APPLICATION_PATH "/application/cache/"

I directly return true in render and assign ,because I don't need the two function , and my app work well. by the way ,when we use bladeone in other unlike laravel , mostly we can set suffix of templete and the left-right quotes , so we can use plugins in code editor, just use laravel blade snippets.
thank you reply so quickly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants