scaffolding - how to modify a variable's value
Asked by Jim Yanko:

I have a case where I need to modify the value of a variable after it is initially set.

The reporting macro is in use to set the variable initially, but there are cases where I would need to further scrub down the value to ensure it is useful.

The following is the current code that initially sets the value of the variable 'app_APPNAME_include'

*{set-data:app_APPNAME_include}{report-block}*

*{local-reporter:variable:app_included}*

*{collection-filter:@self}{text-filter:data:appname \> reference:value\|include=APPNAME}{collection-filter}*

*{local-reporter}*

*{report-body:trim=true}true{report-body}{report-empty}false{report-empty}*

*{report-block}{set-data}*

Once it has been set, the app_APPNAME_include variable will have a value like one of the following...

\* true

\* false

\* truetrue

It is in the case of 'truetrue' where I would want to scrub the value to make it a single occurance of the string 'true' rather than a repeated string.

Does this value modification need to occur within the report block - or as a post processing of the app_APPNAME_include variable that gets set.

I'm anticipating needing to do some type of regular expression match/replace on the value but am not sure how to go about doing so.

Any help is appreciated.


Answers:
author: Jim Yanko
body: This seems to have worked out for me.

```
{set-data:app_APPNAME_include}{report-block:maxResults=1}{local-reporter:variable:app_included}{collection-filter:@self}{text-filter:data:appname  > reference:value|include=APPNAME}{collection-filter}{local-reporter}{report-body:trim=true}true{report-body}{report-empty}false{report-empty}{report-block}{set-data}
```


author: David Peterson
body: Hi Jim,

Just FYI, what is actually happening with {set-data} is that it stores the wiki markup, not the actual 'true/false' (or 'truetrue', or whatever) result. As such, it gets executed every time you view the value on the page, and if you want to process it, you have to render it, otherwise you're processing '{report-block....}', rather than 'true/false'. Hope that makes sense...

It would help if I knew two things:

a) What does 'variable:app_included' contains - how is it set?

b) What are you trying to use the 'app_APPNAME_include' value for? Is it for further processing on the same page? Is it for use on other pages doing a report? Does the value need to survive beyond viewing the current page?

That said, I'll take a guess at providing something that might get you on the right track. Something like this:

```
{report-variable:app_APPNAME_items}
```

```
    {local-reporter:data:app_included}
```

```
        {text-filter:data:appname|include=APPNAME}
```

```
    {local-reporter}
```

```
{report-variable}
```

```
{report-variable:app_APPNAME_include|value=%variable:app_APPNAME_items > collection:is empty > boolean:negate%}
```

This should give you a 'true' or 'false' value in 'variable:app_APPNAME_include'. If you need to store this as a 'Scaffolding' data value via {set-data}, you probably need to use {replace-and-render}, since injection causes issues, and you can't just use the {report-info} here, due to what I said earlier about how {set-data} stores its values. That would look like this:

```
{replace-and-render}
```

```
{replace-item:%app_APPNAME_include%}{report-info:variable:app_APPNAME_include%}{replace-item}
```

```
{replace-body}{set-data:app_APPNAME_include}%app_APPNAME_include%{set-data}{replace-body}
```

```
{replace-and-render}
```

If you don't actually *need* it to be stored in Scaffolding, then skip that stage. Your mileage may vary...

Hope that helps!

Regards,

David Peterson

comments: author: Jim Yanko
body: Thanks for the explanation of the order of ops with {set-data} macro. I was wondering if there was something like that going on under the hood. I previously used twiki for a few years and ran into similar ordering issues there as well).

As for HOW some of the vars are set:

a) variable:app_included is previously set using {report-variable} and

{local-reporter}

b) all variables set are intended for processing on the same page

some are used directly within a {report-table} to display the collected

info and again used to set {replace-item} variables for later use in the

page

As I previously commented, I found a workable solution by setting the maxResults=1 attrib to the report block.

It looks like setting {report-variable:name} to the report results may be more efficient way of setting vars than my current model of a {report-block} inside a {set-data}. I'll look into how I might be able to do things that way instead - as it might save me many lines of wiki syntax down the road.

Can you point me to a good online reference/documentation on the scaffolding macros and injection?


author: Jim Yanko
body: I think I may have found a workable solution by restricting the report results to only one matching row.

*\* {set-data:app_APPNAME_include}{report-block:maxResults=1}*

*{local-reporter:variable:app_included}*

*{collection-filter:@self}*

*{text-filter:data:appname \> reference:value\|include=APPNAME}*

*{collection-filter}*

*{local-reporter}*

*{report-body:trim=true}true{report-body}{report-empty}false{report-empty}*

*{report-block}{set-data}*

Since the report would already be returning only rows that were set to include=true (the report variable:app_included would only contain rows that matched true) I can be fairly certain that any rows of data within have a value of true.

At the moment, this seems to be giving me what I want, a final value of just 'true' or 'false' in the app_APPNAME_include variable.

Will report back after a bit of pressure testing to see if this really does solve this issue.

Of course, if there's another way to do it, I'm open to hear it :-)


author: Jim Yanko
body: Actually, that's not working out 100% for me.

When I define the *myBoolean* variable to *falsefalsefalse* directly, I seem to get valid results from the report block and text filter.

\* Before: {set-data:myBoolean}falsefalsefalse{set-data}

\* After: {report-block}{local-reporter:data:myBoolean}{text-filter:include=.\*true.\*}{local-reporter}{report-body:trim=true}true{report-body}{report-empty}false{report-empty}{report-block}

However, when I tried moving this to work with a scaffold variable -- app_APPNAME_include -- which has it's value previously set to the result of a report, it does not work for me.

\* Before: {get-data:app_APPNAME_include}

\* After: {report-block}{local-reporter:data:app_APPNAME_include}{text-filter:include=.\*true.\*}{local-reporter}{report-body:trim=true}true{report-body}{report-empty}false{report-empty}{report-block}

The two code sample above render as below...needless to say, it is not desirable to have "false" getting translated to "true"

* Before: falsefalsefalse
* After: false

<!-- -->

* Before: false
* After: true


author: Jim Yanko
body: I think I found a way to make this work...if there's a better or more efficient way to do this, please follow up here and let me know...

Wiki code follows:

```
* Before: {set-data:myBoolean}falsefalsefalse{set-data}
```

```
* After: {report-block}
```

```
{local-reporter:data:myBoolean}
```

```
{text-filter:include=.*true.*}
```

```
{local-reporter}
```

```
{report-body:trim=true}true{report-body}{report-empty}false{report-empty}
```

```
{report-block}
```


Tags: scaffolding

variable