Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

HtmlTag helper #6709

Closed

Conversation

nikolaposa
Copy link

New view helper used for rendering <html> tag of a web page, to which some custom attributes can be added dynamically.

Intent

Idea is to have ability to dynamically attach some attributes to the opening HTML tag in a layout. For example:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:og="http://ogp.me/ns#">

Usage

Rendering tag in a layout:

echo $this->html()->openTag(); 
//...
echo $this->html()->closeTag();

Adding attributes:

//single
$this->html()->setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');

//multiple
$this->html()->setAttributes(array(
    'xmlns' => 'http://www.w3.org/1999/xhtml',
    'prefix' => 'og: http://ogp.me/ns#'
));

//shortcut method
$this->html(array(
    'xmlns' => 'http://www.w3.org/1999/xhtml',
    'prefix' => 'og: http://ogp.me/ns#'
));

There is also option to automatically pre-set appropriate namespace attributes, based on currently set DOCTYPE (Doctype helper):

//At some point:
$this->doctype('xhtml');

$this->html()
    ->setUseNamespaces(true) //'xmlns' attribute will be automatically set to 'http://www.w3.org/1999/xhtml'
    ->setAttributes(array(
        'prefix' => 'og: http://ogp.me/ns#'
    ));

* Retrieve object instance; optionally add attributes.
*
* @param array $attribs
* @return \Zend\View\Helper\HtmlTag
Copy link
Contributor

Choose a reason for hiding this comment

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

@return self

Copy link
Author

Choose a reason for hiding this comment

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

Obviously there is no consensus inside ZF2 codebase when it comes to that @return annotation in case of some fluent-interface method which returns self reference. Somewhere you will see self, while on some other place name of the class or a fully qualified class name, like in case of this class. I prefer that variant, so I would rather leave it as is. At least until some coding standard for this purpose is defined. :)

@nikolaposa
Copy link
Author

I'm in doubt whether this is an appropriate view helper name. It is kind of undetermined, neutral. And it can also be confused with existing Html* helpers, like HtmlObject, HtmlPage, HtmlFlash, etc.

Any better ideas?

@froschdesign
Copy link
Member

Why do I need to know the correct URL for XHTML, if I want to use the helper?
The helper should help me!

Please compare with the HeadLink helper.

Otherwise, the helper would be useless. (imo)

@nikolaposa
Copy link
Author

Ok, good point. I will try to make same changes in that direction, but I'd rather limit those new "helping" abilities of this helper to standard, default attributes, like xmlns.

@DASPRiD
Copy link
Member

DASPRiD commented Dec 5, 2014

With that example, how do you add multiple namespaces?

@nikolaposa
Copy link
Author

I suppose that you are referring example from the initial description, so appropriate helper invocation might look like:

$this->html()->setAttributes(array(
    'xmlns' => 'http://www.w3.org/1999/xhtml',
    'xmlns:og' => 'http://ogp.me/ns#'
));

@DASPRiD
Copy link
Member

DASPRiD commented Dec 5, 2014

And what was this for in the example?

    'prefix' => 'og: http://ogp.me/ns#'

@nikolaposa
Copy link
Author

Open Graph protocol: http://ogp.me/. It is recommended that prefix attribute on the HTML tag is used in favor of xmlns, as advised in the top answer on this topic: http://stackoverflow.com/questions/8235687/open-graph-namespace-declaration-html-with-xmlns-or-head-prefix.

@nikolaposa
Copy link
Author

By the way, I have created a ZF2 module which facilitates OG integration (https://github.com/nikolaposa/ZfOpenGraph), which internally relies on the HeadMeta helper. This proposal was created exactly because I lacked ability to dynamically attach necessary OG-specific attribute on the html tag itself, from the context of a OpenGraph view helper that I created.

…r the html tag based on currently set doctype.
@nikolaposa
Copy link
Author

@froschdesign What do you think about that approach with setUseNamespaces() method that I've just added?

@Martin-P
Copy link
Contributor

Martin-P commented Dec 5, 2014

Please choose a better name for setAddDoctypeAttributes(). The method does not add doctype attributes (does a doctype have attributes?) and does not set (overwrite) doctype attributes either. Looking at the code it adds namespaces, perhaps it is better to reffer to that? IMHO setUseNamespaces() is a better name for this.

@nikolaposa
Copy link
Author

Ok, thanks for the feedback, it makes sense.

@Ocramius Ocramius self-assigned this Dec 24, 2014
@Ocramius Ocramius added this to the 2.4.0 milestone Dec 24, 2014
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
Copy link
Contributor

Choose a reason for hiding this comment

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

2015 ;)

Copy link
Author

Choose a reason for hiding this comment

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

Do I really have to modify that? :) I also have some other PRs planned for 2.4.0 within which new classes are added, like this one: #6058, and I haven't got such remark from a code reviewer.

Copy link
Member

Choose a reason for hiding this comment

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

No, it will be done after merge. I don't feel like burdening existing PRs with these changes is needed at all ;-)

weierophinney added a commit that referenced this pull request Feb 23, 2015
weierophinney added a commit that referenced this pull request Feb 23, 2015
weierophinney added a commit that referenced this pull request Feb 23, 2015
weierophinney added a commit that referenced this pull request Feb 23, 2015
Update copyright in new files introduced in #6709
@weierophinney
Copy link
Member

Merged to develop for release in 2.4.

weierophinney added a commit to zendframework/zend-view that referenced this pull request May 15, 2015
weierophinney added a commit to zendframework/zend-view that referenced this pull request May 15, 2015
weierophinney added a commit to zendframework/zend-view that referenced this pull request May 15, 2015
weierophinney added a commit to zendframework/zend-view that referenced this pull request May 15, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants