{"id":444,"date":"2010-03-24T15:49:44","date_gmt":"2010-03-24T13:49:44","guid":{"rendered":"http:\/\/self.mestrona.net\/blog\/2010\/zend-framework-view-helper-to-render-any-html-tag\/"},"modified":"2014-08-03T18:39:18","modified_gmt":"2014-08-03T16:39:18","slug":"zend-framework-view-helper-to-render-any-html-tag","status":"publish","type":"post","link":"https:\/\/s3lf.de\/blog\/2010\/zend-framework-view-helper-to-render-any-html-tag\/","title":{"rendered":"Zend Framework: View Helper to Render any HTML tag"},"content":{"rendered":"<p>Sometimes you might want to render a HTML tag with a lot of dynamic attributes in a view, of you need to create an HTML Element somewhere else were it is unlikely to use HTML with inline &lt;?php tags.<\/p>\n<p>I created a view helper that just takes an array of the attributes and returns the tag. The interesting part is thing is, that this functionality is already hidden in the Zend Framework within Zend_View_Helper_HtmlElement.<\/p>\n<pre>\r\n<pre class=\"textmate-source\">\r\n<pre class=\"sunburst\"><span class=\"source source_php source_php_embedded source_php_embedded_block source_php_embedded_block_html\">&lt;?php\r\n\r\n<span class=\"comment comment_block comment_block_php\">\/*<\/span>\r\n<span class=\"Comment\"> * This helper allows to render any HTML element with attribs from an array.<\/span>\r\n<span class=\"Comment\"> * <\/span>\r\n<span class=\"Comment\"> * @author A. M.<\/span>\r\n<span class=\"Comment\"> * @copyright Public Domain<\/span>\r\n<span class=\"Comment\"> *\/<\/span>\r\n<span class=\"meta meta_class meta_class_php\"><span class=\"storage storage_type storage_type_class storage_type_class_php\">class<\/span> <span class=\"entity entity_name entity_name_type entity_name_type_class entity_name_type_class_php\">Zend_View_Helper_AnyHtmlElement<\/span> <span class=\"storage storage_modifier storage_modifier_extends storage_modifier_extends_php\">extends<\/span> <span class=\"entity entity_other entity_other_inherited-class entity_other_inherited-class_php\">Zend_View_Helper_HtmlElement<\/span> {<\/span>\r\n\r\n\t<span class=\"comment comment_block comment_block_documentation comment_block_documentation_phpdoc comment_block_documentation_phpdoc_php\">\/**<\/span>\r\n<span class=\"Comment\">\t * <span class=\"keyword keyword_other keyword_other_phpdoc keyword_other_phpdoc_php\">@param<\/span> tag The HTML tag, for example img <\/span>\r\n<span class=\"Comment\">\t * <span class=\"keyword keyword_other keyword_other_phpdoc keyword_other_phpdoc_php\">@param<\/span> attribs HTML Attributes<\/span>\r\n<span class=\"Comment\">\t * <\/span>\r\n<span class=\"Comment\">\t *\/<\/span>\r\n<span class=\"meta meta_function meta_function_php\">    <span class=\"storage storage_modifier storage_modifier_php\">public <\/span><span class=\"storage storage_type storage_type_function storage_type_function_php\">function<\/span> <span class=\"entity entity_name entity_name_function entity_name_function_php\">anyHtmlElement<\/span>(<span class=\"meta meta_function meta_function_arguments meta_function_arguments_php\"><span class=\"meta meta_function meta_function_argument meta_function_argument_no-default meta_function_argument_no-default_php\"><span class=\"variable variable_other variable_other_php\">$tag<\/span><\/span>,<span class=\"meta meta_function meta_function_argument meta_function_argument_default meta_function_argument_default_php\"> <span class=\"variable variable_other variable_other_php\">$value<\/span> <span class=\"keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_php\">=<\/span> <span class=\"constant constant_language constant_language_php\">null<\/span><\/span>,<span class=\"meta meta_function meta_function_argument meta_function_argument_array meta_function_argument_array_php\"> <span class=\"storage storage_type storage_type_php\">array<\/span> <span class=\"variable variable_other variable_other_php\">$attribs<\/span> <span class=\"keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_php\">=<\/span> <span class=\"constant constant_language constant_language_php\">null<\/span><\/span><\/span>)<\/span>\r\n    {\r\n    \t<span class=\"keyword keyword_control keyword_control_php\">if<\/span> (<span class=\"variable variable_other variable_other_php\">$value<\/span> <span class=\"keyword keyword_operator keyword_operator_logical keyword_operator_logical_php\">!<\/span><span class=\"keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_php\">=<\/span> <span class=\"constant constant_language constant_language_php\">null<\/span>) <span class=\"keyword keyword_control keyword_control_exception keyword_control_exception_php\">throw<\/span> <span class=\"keyword keyword_other keyword_other_new keyword_other_new_php\">new<\/span> <span class=\"support support_class support_class_php\">Zend_Exception<\/span>(<span class=\"string string_quoted string_quoted_single string_quoted_single_php\">'<span class=\"meta meta_string-contents meta_string-contents_quoted meta_string-contents_quoted_single meta_string-contents_quoted_single_php\">not implemented yet, we can make only &lt;XXXXXX \/&gt; tags right now.<\/span>'<\/span>);\r\n\r\n    \t<span class=\"variable variable_other variable_other_php\">$xhtml<\/span> <span class=\"keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_php\">=<\/span> <span class=\"string string_quoted string_quoted_single string_quoted_single_php\">'<span class=\"meta meta_string-contents meta_string-contents_quoted meta_string-contents_quoted_single meta_string-contents_quoted_single_php\">&lt;<\/span>'<\/span> <span class=\"keyword keyword_operator keyword_operator_string keyword_operator_string_php\">.<\/span> <span class=\"variable variable_other variable_other_php\">$tag<\/span> <span class=\"keyword keyword_operator keyword_operator_string keyword_operator_string_php\">.<\/span> <span class=\"string string_quoted string_quoted_single string_quoted_single_php\">'<span class=\"meta meta_string-contents meta_string-contents_quoted meta_string-contents_quoted_single meta_string-contents_quoted_single_php\"> <\/span>'<\/span>;\r\n    \t<span class=\"variable variable_other variable_other_php\">$xhtml<\/span> <span class=\"keyword keyword_operator keyword_operator_string keyword_operator_string_php\">.=<\/span> <span class=\"variable variable_other variable_other_php\">$this<\/span><span class=\"keyword keyword_operator keyword_operator_class keyword_operator_class_php\">-&gt;<\/span><span class=\"meta meta_function-call meta_function-call_object meta_function-call_object_php\">_htmlAttribs<\/span>(<span class=\"variable variable_other variable_other_php\">$attribs<\/span>);\r\n    \t<span class=\"variable variable_other variable_other_php\">$xhtml<\/span> <span class=\"keyword keyword_operator keyword_operator_string keyword_operator_string_php\">.=<\/span> <span class=\"variable variable_other variable_other_php\">$this<\/span><span class=\"keyword keyword_operator keyword_operator_class keyword_operator_class_php\">-&gt;<\/span><span class=\"meta meta_function-call meta_function-call_object meta_function-call_object_php\">getClosingBracket<\/span>();\r\n        <span class=\"keyword keyword_control keyword_control_php\">return<\/span> <span class=\"variable variable_other variable_other_php\">$xhtml<\/span>;\r\n    }\r\n}<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you might want to render a HTML tag with a lot of dynamic attributes in a view, of you need to create an HTML Element somewhere else were it is unlikely to use HTML with inline &lt;?php tags. I created a view helper that just takes an array of the attributes and returns the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[55],"tags":[],"class_list":["post-444","post","type-post","status-publish","format-standard","hentry","category-zend-framework-2"],"_links":{"self":[{"href":"https:\/\/s3lf.de\/blog\/wp-json\/wp\/v2\/posts\/444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/s3lf.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/s3lf.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/s3lf.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/s3lf.de\/blog\/wp-json\/wp\/v2\/comments?post=444"}],"version-history":[{"count":1,"href":"https:\/\/s3lf.de\/blog\/wp-json\/wp\/v2\/posts\/444\/revisions"}],"predecessor-version":[{"id":499,"href":"https:\/\/s3lf.de\/blog\/wp-json\/wp\/v2\/posts\/444\/revisions\/499"}],"wp:attachment":[{"href":"https:\/\/s3lf.de\/blog\/wp-json\/wp\/v2\/media?parent=444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/s3lf.de\/blog\/wp-json\/wp\/v2\/categories?post=444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/s3lf.de\/blog\/wp-json\/wp\/v2\/tags?post=444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}