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
Remove beginPage() and endPage() in layouts #1507
Comments
The change is because in 1.1 begin and end page were marked with regexp and it was causing weird issues such as placing links before/after title depending on what markup was used. Not sure if it can be simplified somehow w/o using content parsing. |
I don't understand you. The goal is to place functionality of these to function outside layout. Why developers need them in layouts if their work could be done by framework in automatic? |
@klimov-paul Nothing interesting... Just take methods which are in EVERY layout and place their functionality outside user scope (in core yii scripts) |
@egorpromo I think I'm lost. Can you explain your thoughts with code? |
<?php
use yii\helpers\Html;
?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
<meta charset="<?= Yii::$app->charset ?>"/>
<title><?= Html::encode($this->title) ?></title>
<?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>
<div class="container">
<?= $content ?>
</div>
<footer class="footer">© 2013 me :)</footer>
<?php $this->endBody() ?>
</body>
</html> Look at the code above. There is no We can take endPage() from here and place its functionality in (for example) yii\base\Controller::render(): public function render($view, $params = [])
{
$output = $this->getView()->render($view, $params, $this);
$layoutFile = $this->findLayoutFile($this->getView());
if ($layoutFile !== false) {
$output = $this->getView()->renderFile($layoutFile, ['content' => $output], $this);
//We have $output. We can do the functionality that is made by endPage().
$output = $this->endPage($ouput);
//Now we have modified $output.
return $output;
} else {
return $output;
}
} Of course some refactoring needed for realize my idea. But layouts will be more simple. |
better: $output = $this->getView()->endPage($ouput); |
How will you implement this? |
I think there is no problem to implement this. We have yii\base\Controller::render() and public function render($view, $params = [])
{
//some code here
$output = $this->getView()->renderFile($layoutFile, ['content' => $output], $this);
$view=$this->getView();
$view->trigger(self::EVENT_END_PAGE);
foreach (array_keys($view->assetBundles) as $bundle) {
$view->registerAssetFiles($bundle);
}
$output = strtr($output, [
self::PH_HEAD => $view->renderHeadHtml(),
self::PH_BODY_BEGIN => $view->renderBodyBeginHtml(),
self::PH_BODY_END => $view->renderBodyEndHtml(),
]);
unset(
$view->metaTags,
$view->linkTags,
$view->css,
$view->cssFiles,
$view->js,
$view->jsFiles
);
//some code here
} It is not correct to bundle View and Controller but I have made this because I don't know your thoughts about for what are |
How do you get the following code work? $output = strtr($output, [
self::PH_HEAD => $view->renderHeadHtml(),
self::PH_BODY_BEGIN => $view->renderBodyBeginHtml(),
self::PH_BODY_END => $view->renderBodyEndHtml(),
]); |
I think it is possible to remove yii\vase\View::beginPage(), yii\base\View::endPage() and yii\web\View::endPage(). Their functionality can be in renedering methods. |
No need repeating your suggestion. Could you answer my last question? Assuming you get |
@qiangxue Sorry, I am in hurry. We need the functionality in View class only. So we will have all constants of View. |
? I'm not asking about syntaxes. I'm asking "Assuming you get $output, how will you insert head, bodyBegin and bodyEnd into appropriate places in the content?" |
I think we must leave |
If you already write If we do what you suggested here, it would mean |
sorry for interrupting, but will there be some nested layouts in Yii2? like blade in L4? |
We already have it in 1.1 and 2.0. |
can you point to the docs maybe or give an example? i mean like this http://laravel.com/docs/templates |
It's something like this in 1.1: https://github.com/yiisoft/yii/blob/master/demos/blog/protected/views/layouts/column2.php |
|
|
For what we need |
What is the cause to invent them? |
I think they are for layouts only. I don't see other meaning. |
The meaning of endPage() is to register asset files, to substitute markup which generated by |
They are provided so that you can respond to the corresponding events and possibly preprocess or postprocess the content in between. They don't have to be put in a layout file. They also serve as insertion points like
|
I don't have anything against
I don't agree. For what
Yes, I think so. But we will get rid of |
It is possible to trigger event in rendering method lke Controller::render() or in something similar. |
I have answered you. When you render a partial view for AJAX response purpose, there is no layout.
As I said, I am not saying we can't remove them. I'm saying removing them in favor of putting them in render method has more trouble. Since you already write beginBody etc. in a view, why are you against write beginPage? If you insist in removing beginPage, please submit a PR and I will review it. |
My thoughts are simple.
|
I'm closing this issue, as we are clearly not making any more constructive discussion. Please submit a PR if you insist in removing |
I agree. Rails doesn't need all those extra function calls in their layouts. They just use a method to find and insert the Javascript and CSS links. http://guides.rubyonrails.org/layouts_and_rendering.html#asset-tag-helpers I just got done trying to create a simplified layout for use in an iframe, but it was not at all obvious that I needed |
Yii 1.1 worked like that and there were issues with such approach of search and replace. |
how insert this block with method $this->beginPage() ?!
|
There is example of layout. Note the
<?php $this->beginPage() ?>
at the begining and<?php $this->endPage() ?>
at the ending. I think these method calls would be in every layout. These are important calls because without them it is not possible to use$this->head()
,$this->beginBody()
,$this->endBody()
anyway.It is possible to forget them. Another reason is complicated layout file. There are many method calls in layout. It is hard to remember all of them. I propose to remove them from layout file and place their functionality in other place. Then layout files will be a little simple.
I have thoughts.
beginPage()
andendPage()
are needed in layouts only then it is possible to place their functionality in yii\web\Controller::render()What do you think? I think web-develpers don't want to place additional two lines of code every time the layout created.
The text was updated successfully, but these errors were encountered: