This site has been archived and you can no longer log in or post new messages. For up-to-date community resources please visit ezplatform.com

eZ Community » Forums » eZ Publish 5 Platform » Render XML Block Cleanly (EZ 5.4)
expandshrink

Render XML Block Cleanly (EZ 5.4)

Render XML Block Cleanly (EZ 5.4)

Wednesday 11 March 2015 9:34:04 pm - 6 replies

I create a class with an XML block in it and put the following HTML:

<div class="helloWorld">Hello World</div>

I want to load it as HTML when rendered. Meaning, <div> or HTML tags shouldn't be displayed, but be integrated as if they were part of the page.

I tried this:

{{ ez_render_field( content, "body" ) }}

outputs:

<div class="ezxmltext-field"><p><div class="helloWorld">Hello World</div></p><div>

As you can see, there are 2 issues:

1. I don't want my HTML being wrapped in <div class="ezxmltext-field"><p>, just the value. How can I do that?

2. How can I render the value, <div class="helloWorld">Hello World</div> from the XML without the tags <div class="helloWorld"> being printed on the screen with Hello World as HTML?

Modified on Wednesday 11 March 2015 9:45:47 pm by Big Mike

Thursday 12 March 2015 4:16:58 am

Hello Big Mike,

Well one problem I quickly see would be that by default (at least via legacy admin, uncertain of the state of eZ Platform UI) html code by itself may not be stored within an XML Block. HTML and XML just do not mix in this use case.*

Here is a helpful reference of (almost all) the ezxml (underlying input / storage format of xml block content) tags supported in an XML Block. https://doc.ez.no/eZ-Publish/Technical-manual/4.x/Reference/XML-tags

A side question, are you using ezoe, the online editor for XML Blocks or is it just a plain html text area input form element? Without knowing more about which backend your using with or without ezoe it's hard to give more user friendly suggestions / specific helpful suggestions on how instead better implement your content storage.

Another question, what actual html are your storing and for what specific purpose are you trying to store html within content objects? Without a more specific use case example of the content your trying to store, it is very hard to make positive helpful suggestions on the recommended and supported alternatives.

If you want to store HTML code try using an Text Block datatype attribute with display templates customized as needed (note may not be needed, not certain).

Please remember storing html in content object attributes has always been strongly discouraged overall as it is a major violation of the separation of content and design best practices and principles eZ is built upon.

Please don't store html in content object attributes, it's simply the worst solution you could choose to create.

In general the rest of your XML Block rendering question has I think already been answered here: http://share.ez.no/forums/ez-publish-5-platform/xml-to-html

I hope this helps!

Cheers,
Heath

Modified on Thursday 12 March 2015 4:22:28 am by // Heath

Thursday 12 March 2015 7:37:31 pm

Hi Heath,

Thank you for your response, the link definitely helped!

I understand it's bad practice, but I just wanted to know how to output what I put in as html, and how to remove the ezxmltext-field wrapper.

Regards,

Big Mike

Thursday 12 March 2015 10:14:36 pm

Hello Big Mike,

I'm glad I could help!

When you are tempted to store html by itself in XML Block (which I have to say again, does not work and is not supported, so I still don't understand how you would be doing this since you did not answer my questions, please answer my questions!) instead please use the supported alternative, 'Custom Tags'.

https://doc.ez.no/eZ-Publish/Tech...ence/Datatypes/XML-block/Custom-tags

This is the right way to solve the problem cleanly without storing html, it's also more then enough flexible and easy to setup and use. I use custom tags all day long almost every day to solve countless situations the right way and not storing html in content object content which again is -not necessary- (it's the wrong, bad, awful ).

Now I'm really not judging you directly but in general I often hear kracker saying something like "Poor developers jump to storing html in content, while better developers do the required work to -abstract- the storage of content and templating display of that content using custom tags", though he is usually more crass about it and then quotes a rap song blunk.gif Emoticon I mention it simply so he doesn't.

Sigh, alternatively if you refuse to use custom tags ... I have to be honest, even though you should not use this hidden feature, I would be remis not to mention it, you can use the <literal class="html">HTML CODE</literal> the class = html use requires a settings override of content.ini so review the [literal] settings section (it's commented out by default).

Ok. I shared the hidden truth about ezxml that not all developers know about, that said. PLEASE use custom tags instead and do the right thing here. We are all counting on you to use eZ correctly!

Best wishes

I hope this helps!

Cheers,
Heath 

Saturday 14 March 2015 10:27:20 am

Hi.

Heath has explained at length why you shouldn't store HTML in content. 

If despite that you still want to do it, you just need to a) prevent twig from escaping the html, by using the raw() filter b) use a simple textblock field, and not an xmltext field

Since ez_render_field uses a specific rendering mechanism, with twig blocks for each field type, you need to use ez_field_value. It would look like this:

 <span>{{ raw( ez_field_value( content, "html_field" ).text ) }}.</span>

Friday 20 March 2015 3:54:32 pm

@Heath:

Thanks for your help as always - I came from a custom cms that allowed for that, so that's why I was wondering how it would be done in ez publish, but thank you for pointing me to custom tags!

@Bertand:

Thank you for jumping in with an example!

Saturday 21 March 2015 11:23:13 am

An alternative way would be to use a custom tag for your HTML. Implementing those is quite easy, and it would allow you to include HTML in your XML Text blocks.

The template should simply display the custom tag's content using the |raw filter, so that the HTML is not escaped.

But again, it would be better to use custom tags for real, or content embeds, and write proper templates for those happy.gif Emoticon

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from