arnaud-lb / mthaml Goto Github PK
View Code? Open in Web Editor NEWMulti target HAML (HAML for PHP, Twig, <your language here>)
License: Other
Multi target HAML (HAML for PHP, Twig, <your language here>)
License: Other
MtHaml:
- $class = 'bar'
.foo{'class' => $class} Baz
Output:
<div class="foo bar">Baz</div>
MtHaml:
- $attrs = array('class' = 'bar')
.foo{$attrs} Baz
Expected output:
<div class="foo bar">Baz</div>
This is how vanilla Haml works: http://sassmeister.com/gist/466bdb71c5af3414bbd9
Actual output:
<div class="bar">Baz</div>
Support the following filters:
:sass, :textile
Added support for re-ordering imports
Can you please add a new tag and create new release?
Suppose we have the following template:
%body
- $options = [ 'One', 'Two', 'Three', 'Four', 'Five' ]
- if (count($options))
%ul
- foreach ($options as $item)
%li= $item
- else
%div No options!
Now it generates well-indented PHP code like this:
<body>
<?php $options = [ 'One', 'Two', 'Three', 'Four', 'Five' ]; ?>
<?php if (count($options)) { ?>
<ul>
<?php foreach ($options as $item) { ?>
<li><?php echo htmlspecialchars($item,ENT_QUOTES,'UTF-8'); ?></li>
<?php } ?>
</ul>
<?php } else { ?>
<div>No options!</div>
<?php } ?>
</body>
But in fact the output HTML code has some redundant indentation:
<body>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
</body>
It would be nice to have something like this:
<body>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
</body>
This HTML code is much more pretty :)
Currently, the following code is rendered properly in PHP by recognizing that else
is a middle-block statement:
- if ($foo)
A
- else
B
try/catch should be recognized too:
- try something()
A
- catch(Exception $e)
B
A doctype declaration of '!!!' renders to a standard html5 doctype of but anything else gives a blank output.
I've tested with '!!! 5' and '!!! Strict' resulting in the same blank output.
Looking into it and will comment if I find anything enlightening.
I would also like to use MtHaml in very small projects, so a basic lightweight cache implementation would be great. It should compile template files to HTML and save the HTML files to a cache directory. When a (HTML) file is requested, the cache checks if the HAML file has changed and updates the HTML file if neccessary.
Would you mind adding such a cache or would you prefer to keep the cache generic and separate to MtHaml?
Greetings,
Andy
Hi, this project is really interesting, and I would really enjoy to use it in some project.
But it lacks code documentation, so it is really difficult to understand the whole flow of the rendering. Can you provide some in-code documentantion to make easier to integrate it?
Thank you,
Edoardo Tenani
A switch
block like below
- switch ($this->params['action']):
- case 'edit'
= $this->Html->tag('label', 'Username')
= $this->Html->tag('p', $this->data['User']['username'])
- break
- case 'add'
= $this->Form->input('username')
= $this->Form->input('password')
- break
- endswitch
is being rendered to
<?php switch ($this->params['action']): { ?>
<?php case 'edit' { ?>
<?php echo $this->Html->tag('label', 'Username'); ?>
<?php echo $this->Html->tag('p', $this->data['User']['username']); ?>
<?php break; ?>
<?php } ?>
<?php case 'add' { ?>
<?php echo $this->Form->input('username'); ?>
<?php echo $this->Form->input('password'); ?>
<?php break; ?>
<?php } ?>
<?php } ?>
<?php endswitch; ?>
which is produce a fatal error.
If switch
syntax is not supported yet, I would like to patch a fix for that. Please give me some clue on where should I start.
May be necessary to parse all used namespaces and paste them before the function definition in the cache file.
Something like this
Input:
- use yii\helpers\Url
%a{:href => Url::toRoute(['shopping/ping', 'id' => (string)$item->productId, 'category' => (string)$item->primaryCategory->categoryId])} test
Output (cache file):
<?php
use yii\helpers\Url;
function __MtHamlTemplate_3d0018f2469c15708ab379082527f7c90b6b28b9be2e47a1e7d794562143a903($__variables)
{
extract($__variables);
?><a <?php echo MtHaml\Runtime::renderAttributes(array(array('href', (Url::toRoute(['shopping/ping', 'id' => (string)$item->productId, 'category' => (string)$item->primaryCategory->categoryId])))), 'html5', 'UTF-8'); ?>>test</a>
}
See #22
%script{:src => "js/paypal-button.min.js?merchant=XYZZY",
:data-button => "subscribe",
:data-name => "My product",
:data-cancel_return => get_url("merchant/"),
:data-notify_url => get_url("merchant/paypal_confirm.php?id=1&plan=pm"),
:data-a1 => "0",
:data-p1 => $pricing['trial_period'],
:data-t1 => "W",
:data-amount => "1.00",
:data-recurrence => "1",
:data-period => "M"}
Generates
<script <?php echo MtHaml\Runtime::renderAttributes(array(array('src', 'js/paypal-button.min.js?merchant=BHHDAMWL44YUE'), array((:data-button), 'subscribe'), array((:data-name), 'My product'), array((:data-cancel_return), (get_url("merchant/"))), array((:data-notify_url), (get_url("merchant/paypal_confirm.php?id=1&plan=pm"))), array((:data-a1), '0'), array((:data-p1), ($pricing['trial_period'])), array((:data-t1), 'W'), array((:data-amount), '1.00'), array((:data-recurrence), '1'), array((:data-period), 'M')), 'html5', 'UTF-8'); ?>></script>
Which causes an error Parse error: syntax error, unexpected ':'
Just using
%script{:src => "js/paypal-button.min.js?merchant=XYZZY"}
works nicely
Vanilla Haml allows line breaking after commas:
%script{:type => "text/javascript",
:src => "javascripts/script_#{2 + 7}"}
This allows breaking long lines (sometimes with nested objects) into multiple lines.
It drastically improves readability and prevents horizontal scrolling.
I think I got it - I understand how it works now ! It's a nice job, but I'm still not completely sure I understand ...
I get this exception : External exception (app/themes/yuuki/views/News/index.html.taml) : The function "mthaml_attributes" does not exist in "layouts/layout.html" at line 21
This is my layouts/layout.html file : https://gist.github.com/c9db16e337a254b5e046
Thanks for your fast answer ;)
Hey - I got it all working, and it's really nice to have it so far !
I just wanted to know if you plan on supporting empty blocks.
This is my use case :
%script
- block script
"";
I'd like the block just to be empty if the page didn't specified some js :). Thanks for your good work !
The parser seems to hang when fed with the following input:
foo |
^ previous line is just one space
Sometimes I need to handle conditional minified attributes, such as hidden
and it requires interpolation outside of attribute value, because this will still be make element hidden even if attribute value is empty:
%section(id="foo" hidden="#{$hidden ? 'hidden' : ''}")
So, I've tried this and it worked, which makes me happy:
%section(id="foo" #{$hidden ? 'hidden="hidden"' : ''})
But it works only if there's another attribute in front of it, as this doesn't work:
%section(#{$hidden ? 'hidden="hidden"' : ''})
This makes me think - am I using an undocumented feature, which might be lost at some point?
Thanks
- block content
renders to
{% block content %}
instead of
{% block content %}
{% endblock %}
This works however:
- block content
Hi
renders to
{% block content %}
Hi
{% endblock %}
Workaround:
- block content
- endblock
For example, here the resulting <textarea>
value will have unintended whitespace around actual value:
%textarea( many long attributes here )
= $some_long_variable_or_expression
I could write this, but it would introduce too long and wrapping source code lines:
%textarea( many long attributes here ) = $some_long_variable_or_expression
I'm not sure if there are other whitespace significant tags, but %textarea definitely shouldn't be auto-breaked and auto-indented.
I didn't find an Environment $option to disable resulting HTML breaks and indentation globally.
Hi!
Yesterday I'm trying to complie very simple template:
!!!
%html
%head
%title test
%body
On Windows it's compiles, but on Ubuntu (XAMPP for Linux 1.8.1) I've got:
preg_match(): Compilation failed: internal error: previously-checked referenced subpattern not found at offset 358
/opt/lampp/htdocs/modules/haml/vendor/MtHaml/Parser/Buffer.php
...
74 if ($count = preg_match($pattern, $this->line, $match, PREG_OFFSET_CAPTURE)) {
...
Would love to see that
Hi,
There're some issues with the trans tag :
- trans with {'%x%': elem.courses.count}
%x% courses are sharing the same %element%.
In both case I get the following message: « A message inside a trans tag must be a simple text ».
Is there any native way for rendering a partial template?
Even when disabling escaper and setting escape_attrs to false, dynamic attributes are always escaped in resultant php/html file. Here is my environment options:
array('enable_escaper' => false, 'escape_attrs' => false)
Haml:
%div(class="#{body_class()}")
%p test
Output:
<div <?php echo MtHaml\Runtime::renderAttributes(array(array('class', (body_class()))), 'html5', 'UTF-8'); ?>>
<p>test</p>
</div>
I would expect it to work just as disabling escaper worked, where the echo is left in directly. So the output would be closer to <div class="<?php echo body_class(); ?>">
I can't find a way to do equivalent of ruby's
= succeed "." do
%strong last words of a sentence
= surround '(', ')' do
%strong something inside parens
http://haml.info/docs/yardoc/Haml/Helpers.html
Is this supported?
Jade is very similar to haml - http://jade-lang.com/
%p == "hello"
I use MtHaml in IDE to pre-compile haml to clean php. So I added some new useful features IMHO. I can integrate them to MtHaml if community is interested. Please, take a look https://github.com/Zaitsev/MtHamlPHP
%form(action="some_action" method="post" #{form_enctype(form)} )
interpolation here raises exception, i think this is wrong, so what should i do? is there any way to fix it?
haml snippet
%a(href=user.url) Home page
I get rendered ouput is
<a {{ mthaml_attributes([['href', (user.url)]], 'html5', 'UTF-8', false)|raw }}>Home page</a>
but readme.markdown is
<a href="{{ user.url }}">Home page</a>
Is it possible to have non-escaped output from interpolation, for example within :javascript filter?
:javascript
var data = #{$json};
Hi,
this project is really useful and interesting and I think it'd be better with a wiki rather than a single README file, for some examples, split up some explanations, allow users to add their "project using", binding to frameworks, another target and such.
Please continue your great work :).
{:foo=>bar}
becomes
{foo: bar}
I'm working on an older project right now that relies heavily on the Smarty tempting engine. I'm curious if you can point me in the right direction to figure out how to plug Smarty into MtHaml.
Using MtHaml will let us iteratively re-write the templates, while still using Smarty, until we can switch to Twig down the line.
Thanks :)
Hello, I'm looking for a way to interact with the Twig_Environment object, to set options etc :).
Example code :
- if a
- else
1
Gives :
Notice: Undefined property: Twig_TokenStream::$token in blah\twig\lib\Twig\TokenStream.php on line 61
Fatal error: Call to a member function getLine() on a non-object in blah\twig\lib\Twig\TokenStream.php on line 61
Can I disable MtHaml::runtime in tag attributes?
Hi, I have issue using filters with the php convertor, it says javascript filters is missing and I see it loaded by default on the Haml/Enviroment class, even I create and link it manually. do I have to some thing in the grunt file ?
Thanks for the tool.
HAML allows anything in -#
comments, as long as indentation is consistent.
This should compile fine:
-#
first line
indented line
%p(syntax error
When I use single quotes with enctype parameter I get exception like: "Undefined offset: 1"
When I use double quotes - everything is fine.
This issue is only for mthaml v.1.1, with v.1.0.1 - I don't have this pb.
<a href="/demo/secured/hello/admin/World">Hello resource secured for <strong>admin</strong> only.</a>
%a{:href => "/demo/secured/hello/admin/World"}
Hello resource secured for
%strong admin
only.
CRITICAL - Twig_Error_Runtime: An exception has been thrown during the compilation of a template ("Inconsistent indentation: 6 is not a multiple of 4 in AcmeDemoBundle:Secured:hello.html.haml on line 8, column 7") in "/projects/symfony-test/src/Acme/DemoBundle/Resources/views/Secured/hello.html.haml". (uncaught exception) at /projects/symfony-test/app/cache/dev/classes.php line 6880
why is it not possible to put nested haml tags inside of an anchor-tag?
I've noticed this difference while working with both, the ruby implementation and MtHaml. The ruby implementation of Haml supports this kind of comments
/ /end of block
and converts it to
<!-- /end of block -->
MtHaml on the other hand, does converts it to
<!-- <!-- end of block --> -->
Hello folks,
for easy integration with the Laravel framework's view system, we'd need two classes: a compiler adapter for Laravel's compiler interface, and a "service provider" to set up the necessary instances (compiler and an instance of MtHaml\Environment
).
That would allow for people adding this to their Laravel 4 projects just by adding one line to their composer.json
and one line to their config file.
I've got this working locally - if you're interested, I can submit a pull request.
Great package! =)
I'd like to know when you plan to get onto 3.2 features :) ?
(the only real new feature is the deeply-nested hash syntax .foo{:data => {:a => "b", :c => {:d => "e", :f => "g"}}}
to ... data-c-f='g'
).
It's really not the usefulest feature in HAML, but that'd allow you to say "HAML 3.2 compliant" (and it looks damn good for data- attributes)
Hi,
the php-renderer does not respect the whitespace-removal-settings of parent-nodes, which will introduce unwanted whitespaces.
Here's an example:
%a<>
- foreach(array(1,2,3) as $ndx)
%span<>
=$ndx
should render to something like
<a><span>1</span><span>2</span><span>3</span></a>
It looks not that easy to fix the underlying problem, so I replaced all write with raw in my fork. Is there an easy way to fix this?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.