#3 functionality of attributes_string() in template

closed
nobody
5
2012-08-29
2011-03-31
No

it would be great to have support for functionality of attributes_string() directly in templates.

rationale: setting "class" attribute is (for me) most common action & from designers point of view the best place where to do it is in template. setting up "id" is often done for javascript so it would be great to have it in template too.

current state:
<% form begin form.name %>class="w5" id="xyz"<% form end form.name %>

draft (filter like):
<% form input form.name | class="w5" id="xyz" %>

Discussion

  • pavel kropitz

    pavel kropitz - 2011-08-19

    would someone else like to see this implemented or have remarks (Artyom?) about proposed draft?

     
  • pavel kropitz

    pavel kropitz - 2011-09-23

    i mean really simple thing where one should know what he is doing, only put there a string, without combine with attributes inserted in cpp code or evaluating variables inside (but it would be great). something like:

    Index: /home/univerz/projects/animalia/source/animalia/scripts/data_update_cppcms/cppcms_2011-09-23_04-22-48/bin/cppcms_tmpl_cc

    --- /home/univerz/projects/animalia/source/animalia/scripts/data_update_cppcms/cppcms_2011-09-23_04-22-48/bin/cppcms_tmpl_cc (revision 1984)
    +++ /home/univerz/projects/animalia/source/animalia/scripts/data_update_cppcms/cppcms_2011-09-23_04-22-48/bin/cppcms_tmpl_cc (working copy)
    @@ -502,10 +502,11 @@

    class form_block:
    pattern=r'^<%\sform\s+(as_p|as_table|as_ul|as_dl|as_space|input|begin|end)\s+('\ - + variable_match +')\s%>'
    + + variable_match +')\s(?P<attrs>\s[|]\s[^%]|\s*)%>'
    def use(self,m):
    ident=make_ident(m.group(2))
    type = m.group(1)
    + attrs = m.group('attrs')
    global html_type_code
    if type=='input' or type=='begin' or type=='end':
    flags = 'cppcms::form_flags::' + html_type_code;
    @@ -520,7 +521,10 @@
    else:
    output(' _form_context.widget_part(cppcms::form_context::first_part);')
    output(render_command)
    - output(' out() << (%s).attributes_string();' % ident)
    + if attrs.find('|') == -1:
    + output(' out() << (%s).attributes_string();' % ident)
    + else:
    + output(' out() << "%s";' % attrs.replace('|', '').replace('"', '\"').strip())
    output(' _form_context.widget_part(cppcms::form_context::second_part);')
    output(render_command)
    output('}')

    and when i examined the source code i found this small thing resulting in html like (2 spaces before value, 0 after):
    <input type="text" name="_1" value="" class="txt w5"/>

    Index: /home/univerz/projects/animalia/source/animalia/scripts/data_update_cppcms/cppcms_2011-09-23_04-22-48/src/form.cpp

    --- /home/univerz/projects/animalia/source/animalia/scripts/data_update_cppcms/cppcms_2011-09-23_04-22-48/src/form.cpp (revision 1984)
    +++ /home/univerz/projects/animalia/source/animalia/scripts/data_update_cppcms/cppcms_2011-09-23_04-22-48/src/form.cpp (working copy)
    @@ -672,7 +672,7 @@
    void text::render_value(form_context &context)
    {
    if(set()) {
    - context.out() << " value=\"" << util::escape(value()) << "\"";
    + context.out() << "value=\"" << util::escape(value()) << "\" ";
    }
    }

     
  • pavel kropitz

    pavel kropitz - 2011-09-23

    sorry, bad idea to include source code :)

    http://pastebin.com/Ti2XJCuc

     
  • Artyom Beilis

    Artyom Beilis - 2011-09-23

    Few points:

    1. Can you please attach a patch (not inline copy-paste) so I can use patch command (use attachements feature)
    2. I think it would be better to replace pipe symbol with keyword like attr because pipe has different meaning

    I think it would be nice feature

     
  • pavel kropitz

    pavel kropitz - 2011-09-23
    1. OK
    2. Adding attributes to widget can be (partially) viewed as filter, but it is minor thing. Another symbol will be acceptable? Maybe comma? I think that it have better readability:
    <% form input form.first_name, class="txt w5" id="xyz" %> <% form input form.first_name attr class="txt w5" id="xyz" %> <% form input form.first_name | class="txt w5" id="xyz" %> <% form input form.first_name; class="txt w5" id="xyz" %> <% form input form.first_name: class="txt w5" id="xyz" %>

    In c++ we have to type more than in other "web" languages, so why not save a few characters when possible :)

     
  • pavel kropitz

    pavel kropitz - 2011-09-23

    and one "little hack" adding one more .replace('`', '"') & we have possibility of using variables & evaluation inside:

    <% form input form.first_name, class="txt`<< (content.form.first_name.valid() ? `` : ` invalid`) <<`" %>

    but i understand that this is not a good way to do it and personally i don't need this in my current projects, but maybe it can be polished. more common thing is commenting out part of a template temporarilly with <% c++ / %>...<% c++ / %>, it would be great to have shortcut, for example < ... >

     
  • pavel kropitz

    pavel kropitz - 2011-09-30

    svn diff changing text::render_value to correctly place spaces and adds ability of inline inserting attributes to inputs in _tmp_cc

     
  • pavel kropitz

    pavel kropitz - 2011-09-30

    svn diff adding functionality described in my last post is attached

     
  • pavel kropitz

    pavel kropitz - 2011-12-12

    hello, do you plan to merge this, or shoudl i try to find more sophisticated solution?

     
  • Artyom Beilis

    Artyom Beilis - 2011-12-22

    I think that

    <% form block form.name %>class="w5" id="xyz"<% end%>
    

    Would be better because it would allow you
    to make it more flexible, for example

    <% form block form.name %>class="w5" id="<%= somevar %>"<% end%>
    

    Comments? If this is fine I'll add this one

     
  • pavel kropitz

    pavel kropitz - 2011-12-22

    True, but we have that possibility with current

    <% form begin form.name %>class="w5" id="xyz"<% form end form.name %>

    otherwise, it's nearly as short as my solution. maybe i have psychological problem in the background with too much <%%> & idea of one input ~ one tag, so it will be ok.

    <% form block form.name %>class="w5" id="xyz"<% end%> <% form input form.name, class="txt w5" id="xyz" %>
     
  • Artyom Beilis

    Artyom Beilis - 2011-12-29

    Implemented in changeset 2047 in trunk using following syntax.

    <% form block form.name %>class="w5" id="xyz"<% end%>
    
     


Anonymous

Cancel  Add attachments





Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks