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

Support for php namespaces, static methods and yii widgets for Twig #3535

Closed
wants to merge 5 commits into from
Closed

Conversation

ofat
Copy link

@ofat ofat commented May 21, 2014

Here is example of using:

in yii we do:

<?php
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\widgets\Breadcrumbs;
use frontend\assets\AppAsset;
use frontend\widgets\Alert;

AppAsset::register($this);
?>
...
<?php
            NavBar::begin([
                'brandLabel' => 'My Company',
                'brandUrl' => Yii::$app->homeUrl,
                'options' => [
                    'class' => 'navbar-inverse navbar-fixed-top',
                ],
            ]);
...
            echo Nav::widget([
                'options' => ['class' => 'navbar-nav navbar-right'],
                'items' => [...],
            ]);
            NavBar::end();
        ?>
        <div class="container">
        <?= Breadcrumbs::widget([
            'links' => $this->params['breadcrumbs']
        ]) ?>
        <?= Alert::widget() ?>
        </div>

With these functions we can do:

{{ use([
    'yii\\bootstrap\\Nav',
    'yii\\bootstrap\\NavBar',    
    'frontend\\assets\\AppAsset',
    'frontend\\widgets\\Alert'
]) }}
{{ static('AppAsset', 'register', this) }}
...
{{ widget_begin('NavBar', { brandLabel: app.name, brandUrl: app.homeUrl, options: { class: 'navbar-inverse navbar-fixed-top' } }) }}            
...
{{ widget('Nav', {options: { class: 'navbar-nav navbar-right' }, items: ...) }}
...
{{ widget_end('NavBar') }}
...
<div class="container">
    {{ widget('yii\\widgets\\Breadcrumbs', {links: this.params['breadcrumbs']}) }}

    {{ widget('Alert') }}
</div>

Also we can define namespaces globally in config files:

'namespaces' => [
    '\yii\bootstrap\NavBar',    
],

@ofat ofat changed the title Support for php namespaces, static methods and yii widgets Support for php namespaces, static methods and yii widgets for Twig May 21, 2014
@samdark samdark self-assigned this May 21, 2014
@samdark samdark added this to the 2.0 RC milestone May 21, 2014
@samdark
Copy link
Member

samdark commented May 21, 2014

Interesting additions. Need to think about it a bit more.

$class = end($elems);
$this->namespaces[ $class ] = $namespace;
}
return null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it required? PHP returns null by default.

@grachov
Copy link
Contributor

grachov commented Jun 10, 2014

What do you think about such extension? It allows to work with widgets similar to existing form_begin and form_end functions but does not require creating a functions for each widget.

@samdark
Copy link
Member

samdark commented Jun 10, 2014

@grachov I wasn't aware of * usage in such way. Looks interesting. Also I like how everything was moved to a separate class. Looks cleaner.

@grachov
Copy link
Contributor

grachov commented Jun 10, 2014

@samdark As I remember in documentation it's described only for filters, but it's working for functions too. There is also a possibility to register a callback that will be called for each unknown function.
I've updated code a little to simplify the usage.

@samdark
Copy link
Member

samdark commented Jun 19, 2014

@grachov may I use your code for Yii core? I'll attribute it properly, of course.

@grachov
Copy link
Contributor

grachov commented Jun 20, 2014

Yes, sure. It would be great if you find that code useful :)

@samdark samdark closed this in 22884e7 Jun 20, 2014
samdark added a commit that referenced this pull request Jun 20, 2014
@samdark
Copy link
Member

samdark commented Jun 20, 2014

@ofat I've used @grachov's code with some enhancements. That should cover your needs. Please test it and create more issues if anything's missing. Thanks!

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

Successfully merging this pull request may close these issues.

4 participants