Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cro Template as Object

Tags:

raku

cro

The Scenario

I've been using templates in Cro (documented at https://cro.services/docs/reference/cro-webapp-template), and have enjoyed that there are subs in them.

I currently have a 'main' template, and some reports, let's say report1, report2, and report3.

Let's say that, from report3, I want to include an array of report1.

Now, let's say that the reports each have the following subs:

  • init: Some Javascript initialisation code (that should only be included once, no matter how many instances of the report are used)

  • HTML: Some HTML code that should be included for each instance of the report (with a few parameters to differentiate it, but that, due to the restriction of the Javascript framework, may not contain any <script> or <style> tags

  • data: A snippet of Javascript that again has to be repeated for each time the report is included

Currently I have each of the above in a separate sub in the template.

The Problem

Redeclaration of symbol '&__TEMPLATE_SUB__report-initial'.

The Question

While I can pass a report name (eg. "report1") to the main template, what I'm lacking is a way to have the main template call the subs on the report name that has been passed in, since there may be multiple reports involved.

Ideas I've tried

What would be ideal is if I could somehow create a "report" class that inherits from the template, and pass instances of the template class into the main report, and then call the subs as methods on the report class. However, I've been unable to figure out a way to do this.

I can see three likely options here:

  • My difficulty may be that I'm not thinking "The Cro Way". If there's a better way to achieve what I'm trying to do, please let me know
  • There may be a way to achieve what I want, and I've just been unable to understand the documentation (or it may be missing)
  • While unlikely, it's possible that Cro hasn't been designed with this kind of possibility in mind.

Any help anyone can provide would be appreciated.

Thanks!

Edit: I think a macro that can have multiple (named) "bodies" would solve the problem.

like image 674
Timothy Nelson Avatar asked Nov 12 '21 09:11

Timothy Nelson


People also ask

What is a Cro program?

What is a CRO program? A CRO program, also known as a CRO plan, is a digital marketing strategy that businesses develop for improving the conversion rate of their website or app. CRO programs involve analyzing an entire website to find obstacles to conversion and then optimizing web pages to improve conversion.

How do I get Started with Cro?

Let's get started! Access your free CRO action plan. Make a copy of the plan and start learning about your users to increase your conversion rate. It's a shared Google doc — we won't ask for your email! The 3 steps to increase conversion rate: If you want to convert your traffic, you need to understand why people are reaching your website .

How do I make a copy of my Cro action plan?

Make yourself a copy by clicking on 'Make a copy' below, and when you get on the document, clicking on File > Make a copy. Let's get started! Access your free CRO action plan. Make a copy of the plan and start learning about your users to increase your conversion rate. It's a shared Google doc — we won't ask for your email!

How do I use objects as a parameter in azure templates?

When using objects as a parameter in Azure Resource Manager templates you may want to include them in a copy loop, so here is an example that uses them in that way: This approach becomes very useful when combined with the serial copy loop, particularly for deploying child resources.


2 Answers

my initial response to your question is "please can you provide a minimal reproducible example of your code so that we can get a deeper view of the context and have something that we can experiment with"

my current understanding of what you need is "to use raku style classes & objects (with callbacks) in a Cro template setting" - and that the standard ways of doing this such as associative access to a nested topic variable are too limited

in itself, this is not necessarily a weakness of raku / Cro in that the power of a template slang needs to be limited to avoid potential security issues and, as with most template systems, is a bit more prosaic than a full blown coding language

my guess is that Cro template-parts which can chunk up web parts and steps in and out of the (real raku) root block can, depending on how you chunk things up, handle the report data structure that you describe - have you tried this?

if this is still not tenable, there are a couple of ways to expand the options such as dependency injection and route handlers

like image 31
p6steve Avatar answered Oct 25 '22 00:10

p6steve


It looks like &__TEMPLATE_SUB__report1-initial is a global that is redeclared when you import report1 into report3. May I suggest to try and use template fragments instead of the whole template?

like image 133
jjmerelo Avatar answered Oct 24 '22 23:10

jjmerelo