Aura.Html icon indicating copy to clipboard operation
Aura.Html copied to clipboard

Add Helper\Title::get()

Open jakejohns opened this issue 9 years ago • 4 comments

This makes it so I can get the text of the title property without the HTML for use elsewhere in a layout. For example:

<?php
// page.php

$this->title('Page Title');
//...
<?php
// layout.php

$this->metas()->add(
    'name' => 'title',
    'property' => 'og:title',
    'content' => $this->title()->get()
);

$this->title()->prepend('Site Title: ');
//...

Thoughts?

jakejohns avatar Jan 29 '16 18:01 jakejohns

:+1: for this. not spamming, but I understand sometimes this will help :) .

harikt avatar Jan 29 '16 19:01 harikt

(/me nods)

If someone uses setRaw() or appendRaw(), and then get(), does that mess up your use case?

pmjones avatar Jan 29 '16 21:01 pmjones

so @pmjones so may be you are proposing to use $this->escaper->html() inside get , and getRaw() a new method? So if they need raw data ?

harikt avatar Jan 30 '16 04:01 harikt

Ah.... There is an issue here, isn't there?

Well, I think there's some options.

1. Assume escaped, users responsibility

I think the idea is that when adding to the title, one is assuming they are always keeping the title in a valid state, namely escaped for html. So the use case above should be rewritten as:

$helper->metas()->add(
    [
        'name' => 'title',
        'property' => 'og:title',
        'content' => htmlspecialchars_decode($helper->title()->get())
    ]
);

However, this is a little weird as the HTMLEscaper may have $flags and you'd want to get that from the escaper and pass them to htmlspecialchars_decode.

2. Assume escaped, add ::get and ::getRaw

To avoid the onerous need to manually call decode, and get the flags, etc... Perhaps the HTMLEscaper could use an additional method (unescape, or decode, or something) which would allow for the reversing of the encoding.

// HTMLEscaper
// ...

public function unescape($escaped)
{
    return htmlspecialchars_decode(
        $escaped,
        $this->flags
    );
}

The Title helper could use this method internally in getRaw:

// Helper\Title
// ...
public function get()
{
    return $this->title;
}

public function getRaw()
{
    return $this->escaper->unescape($this->title);
}

3. Some other more convoluted paradigm

Alternatively, I guess we could try to store the 'parts' added to the title string in some sort of structure which identified the 'escaped/unescaped' nature of each piece and held off on escaping until later. I'm not sure this would be completely useful though, as I think it implies that the user might want a title in some kind of 'invalid state' at some point.

I think I like the 2nd option. Thoughts?

jakejohns avatar Feb 07 '16 19:02 jakejohns