Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
52 posts Page 4 of 6
Previous 1, 2, 3, 4, 5, 6 Next
-----------SOLVED-----------


@KK,

Apparently, the main issue is with the way I set up the page id.

Code: Select all
<cms:set page_to_edit="<cms:pages masterpage=k_template_name page_name=q_pn><cms:show k_page_id /></cms:pages>" />
...
page_id=page_to_edit


Even if page_to_edit outputs 70, it does not find the page while if i manually set the page id to 70:
Code: Select all
page_id='70'

It does trigger the submit_success.
Dear all,

I am dynamically generating a form and the number of fields is not always the same, therefore, I have to generate dynamic names for the input fields.

For this, i am defining a field count and setting it to 0,
Code: Select all
<cms:set field_count='0' />


I am setting the name of the input field, followed by the field count number to make unique names,
Code: Select all
<cms:set s_field="<cms:concat 'field_' field_count />" scope='global' />


I am creating the field with that name,
Code: Select all
<cms:input name=s_field type='text' />


And at the end of every loop, I am incrementing the field count by 1.
Code: Select all
<cms:incr field_count '1' />


Everything is working as expected but the main issue that i have is when I need to pull the value of that input field:

Code: Select all
                <cms:capture into='my_data.' is_json='1'>
                   {
                      "field" : <cms:escape_json><cms:show frm_s_field /></cms:escape_json>
                   }
                </cms:capture >


I know that under normal circumstances it should be frm_s_field if the name would be hardcoded but in this current situation, this will not work since s_field is not the name, it holds the field name.
@aleksandru,
The answer is to use <cms:get> that, unlike <cms:show>, takes a string as parameter and so you can easily craft that string e.g.
Code: Select all
<cms:get "frm_<cms:show s_field />" />

More details - https://docs.couchcms.com/tags-reference/get.html

Hope this helps.
KK wrote: @aleksandru,
The answer is to use <cms:get> that, unlike <cms:show>, takes a string as parameter and so you can easily craft that string e.g.
Code: Select all
<cms:get "frm_<cms:show s_field />" />

More details - https://docs.couchcms.com/tags-reference/get.html

Hope this helps.


Good day KK,

]Apparently, the show tag does not work inside the get tag.
Using :
Code: Select all
<cms:get 'frm_<cms:show s_field />' />

Or even just:
Code: Select all
<cms:get '<cms:show s_field />' />

Will not display the value of s_field, it will not display anything...
KK wrote: Quotes, quotes ...
https://docs.couchcms.com/concepts/sett ... eters.html

Does that help?


I am not sure what I am doing wrong, single quotes or double quotes, the output should be the same, right ? It is either that or I need to step away for a few minutes and get a coffee. :lol: :lol:

As the following, with double quotes still leaves the repeatable region empty or does not display anything if I place it anywhere on the page.

Code: Select all
"field" : <cms:escape_json><cms:get "frm_<cms:show s_field />"/></cms:escape_json>
As far as the code goes, it definitely works; following is a test -
Code: Select all
<cms:set frm_field_0='Hello World' />

<cms:set field_count='0' />
<cms:set s_field="<cms:concat 'field_' field_count />" scope='global' />

"field" : <cms:escape_json><cms:get "frm_<cms:show s_field />"/></cms:escape_json>

Output:
"field" : "Hello World"

So now if you are not getting the expected output, you need to find any logical error in your code that could be the cause.
KK wrote: As far as the code goes, it definitely works; following is a test -
Code: Select all
<cms:set frm_field_0='Hello World' />

<cms:set field_count='0' />
<cms:set s_field="<cms:concat 'field_' field_count />" scope='global' />

"field" : <cms:escape_json><cms:get "frm_<cms:show s_field />"/></cms:escape_json>

Output:
"field" : "Hello World"

So now if you are not getting the expected output, you need to find any logical error in your code that could be the cause.


Nice, thank you very much for the test.
I really need to figure it out but it will be a little bit hard because, if I just change the field to :

Code: Select all
"field" : <cms:escape_json><cms:show frm_field_0 /></cms:escape_json>


...to get only the value from the first field, it works. Yes, it is not dynamic and it is adding the same value to all of the created fields but with just this change, it works.
This means that everything works as expected and there is no logical error in the rest of my code, or at least it is a really, really hidden one or stupid one. :lol: :lol: :lol:
aleksandru wrote: This means that everything works as expected

With a shitton of logging tags (dump, dump_all, log) and funcs (CmsFu » Logs) one should have no problem spotting an out-of-scope variable. Cover each line with tests and you'll know the issue firsthand. ☺
I have changed the code a little bit to get rid of wildly setting variables and figured out the problem and yes, it is a simple logic problem. Take the more realistic code for example:
Code: Select all
<cms:repeat count='2' >
           <tr>
                <td class="tg-0lax"><cms:show k_page_title/><cms:show field_count /></td>
                <td class="tg-0lax"><cms:input name="<cms:concat 'nominal_' field_count />" type='text' /></td>
                <td class="tg-0lax"><cms:input name="<cms:concat 'um_' field_count />" type='text' /></td>
                <td class="tg-0lax"><cms:input name="<cms:concat 'new_condition_upper_' field_count />" type='text' /></td>
                <td class="tg-0lax"><cms:input name="<cms:concat 'new_condition_lower_' field_count />" type='text' /></td>
                <td class="tg-0lax"><cms:input name="<cms:concat 'used_condition_upper_' field_count />" type='text' /></td>
                <td class="tg-0lax"><cms:input name="<cms:concat 'used_condition_lower_' field_count />" type='text' /></td>
              </tr>
                <cms:if k_success >
                    <cms:capture into='my_data.' is_json='1'>
                       {
                          "field" : <cms:escape_json><cms:show k_page_title /></cms:escape_json>,
                          "nominal" : <cms:escape_json><cms:get "frm_<cms:show "<cms:concat 'nominal_' field_count />"/>"/></cms:escape_json>,
                          "um" : <cms:escape_json><cms:get "frm_<cms:show "<cms:concat 'um_' field_count />"/>"/></cms:escape_json>,
                          "new_condition_upper" : <cms:escape_json><cms:get "frm_<cms:show "<cms:concat 'new_condition_upper_' field_count />"/>"/></cms:escape_json>,
                          "new_condition_lower" : <cms:escape_json><cms:get "frm_<cms:show "<cms:concat 'new_condition_lower_' field_count />"/>"/></cms:escape_json>,
                          "used_condition_upper" : <cms:escape_json><cms:get "frm_<cms:show "<cms:concat 'used_condition_upper_' field_count />"/>"/></cms:escape_json>,
                          "used_condition_lower" : <cms:escape_json><cms:get "frm_<cms:show "<cms:concat 'used_condition_lower_' field_count />"/>"/></cms:escape_json>
                       }
                    </cms:capture >
                </cms:if>
                <cms:incr field_count '1' />
</cms:repeat>


In this code, while the names of the input fields are being set correctly, with the field_count value, the names within the capture block are not because they are only being set upon k_success which means that all of them will have the same value and still, since i am incrementing at the end of the loop, If i start with 0 and loop it two times, i will end up with field_count=2 when actually the names will be nominal_0 and nominal_1 [but this can easily be countered by incrementing at the beginning of the loop and not at the end].

I am not sure at this point how I can make the numeric values remain stable while using them in k_success. Any ideas anyone ?
Previous 1, 2, 3, 4, 5, 6 Next
52 posts Page 4 of 6