arnaud-lb / MtHaml

Multi target HAML (HAML for PHP, Twig, <your language here>)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MtHaml::runtime

huynhminhson opened this issue · comments

Can I disable MtHaml::runtime in tag attributes?

Can I disable MtHaml::runtime in tag attributes?
Now, the following code segment

%a(href=$user)

will be rendered to

>Home page

This is required to render the attribute as defined by HAML :

  • if $user is false, attribute is not rendered at all (usecase: 'checked', 'selected' attributes)
  • if $user is true, attribute is rendered as boolean attribute (without value; same usecases)
  • else, it's rendered normally

See boolean attributes in the HAML spec.

There is also special handling of data, class, id attributes.

The compiler tries not to use the Runtime attribute renderer when possible; there may be room for improvement though.

This happens to me even if there is nothing to escape.. Is this intentional?

%img(src= "../img/logo.png", id="brand")
becomes
<img <?php echo MtHaml\Runtime::renderAttributes(array(array('src', ("../img/logo.png",)), array('id', 'brand')), 'html5', 'UTF-8'); ?>>

Is there a good way to disable this? Id rather not have this dependancy

@ktaragorn, try with %img(src="../img/logo.png" id="brand") (no comma, no leading space). This one generates plain html:

<img src="../img/logo.png" id="brand">

or in xhtml mode:

<img src="../img/logo.png" id="brand"/>

The reason is that in html-like attribute syntax, the comma is not treated as having any special meaning; and ends up being part of the attribute value. Since the resulting attribute value "../img/logo.png", has a trailing comma, it cannot be recognised as a constant expression (e.g. a quoted string), and MtHaml considers that it's some target-language expression, whose result is unknown until execution.

Hi @arnaud-lb thanks for the fast response! Yup I tried this last night, and it fixed it for this tag, but my form and my input tags were not fixed. I will try again tonight.

Ha, it appears that single quotes triggers this, but double does not. When used in attr values i mean. Any reason for this? or bug?

@ktaragorn : MtHaml only recognises double-quoted strings natively. Everything that is not recognised natively is left to the responsibility of the target language, and is considered to be abstract code. The output of abstract code cannot be guessed at compile time, hence the need to handle this at runtime using the Runtime helpers.

Adding support for recognising single quotes shouldn't be hard, though. Anyone willing to implement this ?

@reedboat @huynhminhson , as explained in a previous comment, this is required in order to fully support HAML features. However, it may be possible to add an option to forcefully disable the runtime helpers, at the price of disabling these features.

With this

%img(src="../img/logo.png" id="<?php function(); ?>")

i get

  <img src="../img/logo.png" id="<?php function(); ?">

If i use #{} i get the code MtHaml\Runtime::renderAttributes ecc

Closing as not a bug - runtime rendering of attributes is required in order to fully support HAML features like special handling of data- attributes and boolean attributes. I will accept pull requests for making it optional, though, if there is a need for such option.