-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[BUG]: TagFactory __invoke and Html\Helper\Meta,Link,Script,Style. Store reset at each call #16441
Comments
I am not sure if this is a bug to be honest. Doing this: $this
->tag
->meta()
->addName('robots', 'index, nofollow')
->addName('author', 'Me')
; will produce
If you change the example to this: // IndexController.php
$this->tag->meta()->addName('robots', 'index, nofollow');
// index.phtml
<?php $this->tag->meta()->addName('author', 'Me'); ?>
<?= $this->tag->meta(); ?> then it should work as expected. The chaining makes the A test that I wrote is as follows $escaper = new Escaper();
$meta = new Meta($escaper);
$meta
->addName('robots', 'index, nofollow')
->addName('author', 'Me')
;
$expected = ' <meta name="robots" content="index, nofollow">'
. PHP_EOL
. ' <meta name="author" content="Me">'
. PHP_EOL;
$actual = (string) $meta;
$I->assertSame($expected, $actual);
$meta = new Meta($escaper);
$meta->addName('robots', 'index, nofollow');
$expected = ' <meta name="robots" content="index, nofollow">'
. PHP_EOL
. ' <meta name="author" content="Me">'
. PHP_EOL;
$actual = (string) $meta->addName('author', 'Me');
$I->assertSame($expected, $actual); As you can see it works with either setting the I am wondering, can you try something like this and let me know?
|
I tried. // IndexController.php
$this->tag->meta()->addName('robots', 'index, nofollow');
// index.phtml
<!-- start -->
<?php $this->tag->meta()->addName('author', 'Me'); ?>
<?= $this->tag->meta(); ?>
<!-- end --> The results are below. <!-- start -->
<!-- end --> Currently, I think it is possible to obtain the expected output with the following. // index.phtml
<!-- start -->
<?= $this->tag->newInstance('meta'); ?>
<?= $this->tag->meta()->addName('author', 'Me'); ?>
<!-- end --> In this case, it will be as follows. <!-- start -->
<meta name="robots" content="index, nofollow">
<meta name="author" content="Me">
<!-- end --> Although the method name is "new"Instance, it returns the registered "old"Instance. PHP 8.1.20 |
Since you can only have one TagFactory for each service (tag), you cannot write multiple tags at the same time unless you retain the instance of the service you created first. If you use class IndexController extends \Phalcon\Mvc\Controller
{
public function beforeExecuteRoute($dispatcher)
{
$this->tag->meta()->addName('viewport', 'idth=device-width, initial-scale=1.0');
}
public function indexAction()
{
$this->tag->meta()->addName('robots', 'index, nofollow');
} Hasn't this been pointed out as an odd specification (probably a bug)? If you want to use it like this, you need to do the following. class IndexController extends \Phalcon\Mvc\Controller
{
protected $meta;
public function beforeExecuteRoute($dispatcher): void
{
$this->meta = $this->tag->meta()->addName('viewport', 'idth=device-width, initial-scale=1.0');
}
public function indexAction()
{
$this->meta->addName('robots', 'index, nofollow');
} Honestly, I think it's very inconvenient. |
Yes it's not conveniant. I try another workaround, //BaseController (used for common code for others controller
$this->view->meta = $this->tag->meta();
//IndexController extends BaseController
$this->view->meta->addName('description', 'foo');
//index.phtml works with this->view ok $meta
<?= $this->view->meta; ?> //works
<?= $this->view->meta->addName('foo', 'bar'); ?> //works |
Don't know, Niden said on discord :
(https://discord.com/channels/310910488152375297/310910488152375297/1104470868131852392) So if it's intended to work like Title, we can expect to be able to do $this->tag->meta() on different file without loosing data ? |
Similar to There is no problem with clearing it when you intend, but since it is not common, I think it is acceptable to call it in a special way. Conversely, you may want to output the same tag with different content regardless of what is registered in <?= $this->tag->meta()->addName('something', 'foo'); ?> is very intuitive and easy to use, but it destroys registered information, so I don't think there is any other way than to write the HTML directly instead of using a helper. |
Looking at the manual, it seems that CSS and JavaScript are based on the use of assets. |
Currently, I think this is possible by using |
Yes I'm ok with that. So currently I'm using this solution : //BaseController (used for common code for others controller
$this->view->meta = $this->tag->meta();
//IndexController extends BaseController
$this->view->meta->addName('description', 'foo');
//index.phtml works with this->view ok $meta
<?= $this->view->meta; ?> //works
<?= $this->view->meta->addName('foo', 'bar'); ?> //works I found it cleaner / more readable. |
@yannux |
@yannux What is the status of this? I saw you made some changes as a workaround by assigning the Can you try this: //IndexController extends BaseController
$this->tag->meta()->addName('description', 'foo');
//index.phtml
<?= $this->tag->meta()->addName('foo', 'bar'); ?>
<?= $this->tag->meta(); ?> |
Hi @niden sorry I was focus in test and exchange with s-ohnishi and forget to answer you :( The result is <!-- start $this->tag->meta()->addName('foo', 'bar') -->
<meta name="foo" content="bar">
<!-- end $this->tag->meta(); --> Adding $this->meta() to a view variable is a simple/good workaround own our side. |
This is so weird. I will do some more investigation on this. You should be seeing both meta tags. Sigh |
Thank you for testing |
Most likely yes. That is where I was leaning |
Initially I was thinking of introducing a |
I think you need a method to remove elements in the store more than a method to clear the store. |
There were other things that were needed. |
Just some ideas...
Is that method supposed to check if an instance already exists and is attached?
|
It does not create a new instance.
yes. Returns an instance that already exists (registered as a service).
yes. I think it's returning an instance of the same TagFactory. I think the first step to solving the problem is to avoid clearing the registered data when returning the instance. |
Resolved in #16455 Added a At the moment manipulation of entries (remove for instance) is not feasible since they do not have a unique name. That could be a NFR for the future. |
Describe the bug
When calling multiple times the Meta helper, previous stored values disappear.
To Reproduce
Only output
Another quick example
Expected behavior
A clear and concise description of what you expected to happen.
Must output
Details
Additional context
Each time you call $this->tag->meta() AbstractSeries::__invoke reset the store
The text was updated successfully, but these errors were encountered: