Dear Couchmembers,
I've just recently learned how to take actions while persisting data in the admin panel. For instance, upon creating a blog post, I may want the site to send an email, persist some change to a related template, or log an update event.
Be warned, it is a hacky way of doing it
, but it does work! If anyone happens to know a better way, I would love to hear it! Until then, here's what I have so far. Maybe someone has something more elegant they can share.
- - - - - - -
So, <cms:persist /> seems to be *strictly* a self-closing tag, unlike <cms:db_persist /> and <cms:db_persist_form /> which you can wrap if you want (<cms:db_persist> // do things here </cms:db_persist>).
However, It turns out we can still sneak some logic in as an argument to a field parameter! Just use a fake one so you don't overwrite anything real.
You can also take actions only under certain conditions.
Because we're cramming the logic into double quotes, it is difficult to work with variables but not impossible.
To combine variables, use <cms:capture>
- - - - - - -
Like I said, this is really hacky, but being able to take action upon <cms:persist /> seems powerful. I used <cms:log /> in the examples above, but you could use a number of other couch tags like <cms:send_mail> or <cms:db_persist> to send emails, or persist data to a related template, etc.
Maybe there's a better way. Would love to hear thoughts or feedback
I've just recently learned how to take actions while persisting data in the admin panel. For instance, upon creating a blog post, I may want the site to send an email, persist some change to a related template, or log an update event.
Be warned, it is a hacky way of doing it


- - - - - - -
So, <cms:persist /> seems to be *strictly* a self-closing tag, unlike <cms:db_persist /> and <cms:db_persist_form /> which you can wrap if you want (<cms:db_persist> // do things here </cms:db_persist>).
However, It turns out we can still sneak some logic in as an argument to a field parameter! Just use a fake one so you don't overwrite anything real.
- Code: Select all
// Log an update event
<cms:persist
field_that_does_not_exist = "
<cms:log 'logs/admin-updates.txt' value='Updated the record' />
"
/>
You can also take actions only under certain conditions.
- Code: Select all
// Conditional actions
<cms:editable
name='tracking_number'
label='Tracking number'
type='text'
/>
<cms:persist
zzz = "
<cms:if (frm_tracking_number && frm_tracking_number != tracking_number)>
<cms:log 'logs/tracking.txt' value='Tracking number updated' />
</cms:if>
"
/>
Because we're cramming the logic into double quotes, it is difficult to work with variables but not impossible.
- Code: Select all
// Custom variables aren't recognized outside of <cms:persist /> tag
// This won't work
<cms:set my_custom_var='Tracking info updated' scope='global' />
<cms:persist
zzz = "
<cms:if (frm_tracking_number && frm_tracking_number != tracking_number)>
<cms:log 'logs/tracking.txt' value=my_custom_var />
</cms:if>
"
/>
// Instead, you need to set it *inside* the fake field parameter. This does work.
<cms:persist
zzz = "
<cms:if (frm_tracking_number && frm_tracking_number != tracking_number)>
<cms:set my_message='Tracking info updated' />
<cms:log 'logs/tracking.txt' value=my_message />
</cms:if>
"
/>
To combine variables, use <cms:capture>
- Code: Select all
// Combine variables
<cms:persist
zzz = "
<cms:if (frm_tracking_number && frm_tracking_number != tracking_number)>
<cms:capture into='my_message'>Tracking info updated: <cms:show frm_tracking_number /></cms:capture>
<cms:log 'logs/tracking.txt' value=my_message />
</cms:if>
"
/>
- - - - - - -
Like I said, this is really hacky, but being able to take action upon <cms:persist /> seems powerful. I used <cms:log /> in the examples above, but you could use a number of other couch tags like <cms:send_mail> or <cms:db_persist> to send emails, or persist data to a related template, etc.
Maybe there's a better way. Would love to hear thoughts or feedback
