WordPress.org

Ready to get started?Download WordPress

Forums

WP-LESS
using php inside a .less file (12 posts)

  1. obvio
    Member
    Posted 4 years ago #

    hi there,

    that's a really great plugin and I'm super happy to use it, I'm sure it'll cut my dev time quite a bit, thanks!

    what I wanted to ask is if it's possible to have php values echoed inside the .less file, I'm considering using it in conjunction with some theme-options page where the user would be able to choose a colour/size etc. right now I get an error..

    what do you think about it?

    thanks again!

    http://wordpress.org/extend/plugins/wp-less/

  2. obvio
    Member
    Posted 4 years ago #

    bump?

  3. WraithKenny
    Member
    Posted 3 years ago #

    I was hoping for an implementation would take options (colors, font-family, padding, etc.) and pass those to the lessphp compiler, which on option update (wp api) would compile to static css, which would be included in the theme, rather then the .less file. Would work better with the boot-strap I think... I know how to do options pages, and I think I can do the boot-strap, but like obvio, I was wondering how tough passing php variables to lessphp is?

  4. oncletom
    Member
    Plugin Author

    Posted 3 years ago #

    Hello,

    the LESS API can't let us doing such things. So sorry, we can't plug directly.

    However, there are many hooks and filter you can deal with to achieve your goal. I realized it's a bit complicated to do that so I added a wp-less_compiler_parse, plus 2 new methods on the compiler, getBuffer and setBuffer.

    This way, you can alter to loaded LESS file before it is pared and outputed in a pure CSS file:

    @siteurl: %%bloginfo_url%%;
    @cdn_doman: %%cdn_domain%%;
    @bgcolor: %%yourtheme_bgcolor%%;

    body{
    background: @bgcolor url('@cdn_domain/specialdir/background.png');
    }

    And in a PHP function file, or whatever name it has:

    add_action('wp-less_compiler_parse', 'my_plugin_less_replace_vars');

    function my_plugin_less_replace_vars(WPLessCompiler $compiler){
    $compiler->setBuffer(str_replace(
    array(
    '%%bloginfo_url%%'
    '%%cdn_domain%%',
    '%%yourtheme_bgcolor%%',
    ),
    array(
    get_bloginfo('url'),
    aClass::aSpecialMethod(),
    get_theme_background_color(),
    ),
    $compiler->getBuffer()
    ));
    }

  5. oncletom
    Member
    Plugin Author

    Posted 3 years ago #

    If needed, I can also add some handy methods to cleanup existing compiled files.

    So in one line of code, with your own plugin, you could manage to purge static files.

    Keep me in touch.

  6. timarthurlee
    Member
    Posted 3 years ago #

    When I try to use this method of passing variables to the .less file I get this:
    parse error: failed at @siteurl: %%bloginfo_url%%; line: 2

  7. oncletom
    Member
    Plugin Author

    Posted 3 years ago #

    This means your replacements are done after the parsing of LESS file. It should be before any content is parsed.

    If you do it with the filter, it's too late (it's the returned parsed content).
    If you do it with the action, you handle the Compiler before it's content get parsed.

    Check this line: http://plugins.trac.wordpress.org/browser/wp-less/trunk/lib/Compiler.class.php?rev=313122#L36

  8. timarthurlee
    Member
    Posted 3 years ago #

    No matter where I put the action call, I get that same error. I've tried placing it before the less sheet is processed as well as after. It doesn't seem to be replacing the content, I've used your exact example and received these results. Any help would be appreciated.

    Thanks,

  9. oncletom
    Member
    Plugin Author

    Posted 3 years ago #

    Upcoming 1.3.1 release will fix the problem.
    I just changed action names, to avoid collisions with filter.

    I may alter some things later on this part, to avoid accessing the buffer this way.

  10. timarthurlee
    Member
    Posted 3 years ago #

    What would the syntax be for the new modifications to pass these variables? I've been trying several things using the new action hook names but I've not been able to get anything different than before.

    Thanks,

  11. oncletom
    Member
    Plugin Author

    Posted 3 years ago #

    It's exactly the same code, except you have to hook on wp-less_compiler_parse_pre.

    I've tested and it worked.

  12. timarthurlee
    Member
    Posted 3 years ago #

    I did get it working, but I had to make two changes in order to make it work:
    1. It complains about parsing errors with:
    @siteurl: %%bloginfo_url%%;
    but it works with:
    @siteurl: '%%bloginfo_url%%';

    2. Variables that are in imported files do not get replaced.

    In order to pass variables to different files I have to import the files, if I enqueue them each individually, the variables do not get passed.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags