Jump to content

Record template: If database id =, then ...


Meddysong

Recommended Posts

My programming limitations are getting in the way. I know what I want to achieve, I believe I know the approach to take, but I don't know how to code it.

Several of my databases use the same record display. They also happen to have identical fields with similar keys. Those field keys take the form database-determined-prefix_key-name. Some examples: articles_definitions, readers_definitions, places_definitions.

In order to make my code neater, I'd like to assign those database-determined prefixes to a variable and then look up the fields using the variable to replace the prefix:

{$record->customFieldDisplayByKey('$prefix_definitions', 'display')|raw}

I'm not sure yet how to change that code to factor in the variable. But before I get to that, I want to create $prefix.

Since I have several databases, I think I need an array formula, which would be something like "here's a sequence of database ids; now match them with this sequence of prefix names depending on their relative position". I could write them like this:

{{if $record::database()->id == 1}}{{$prefix = 'artikoloj';}}{{endif}}
{{if $record::database()->id == 7}}{{$prefix = 'legantoj';}}{{endif}}
{{if $record::database()->id == 8}}{{$prefix = 'loke';}}{{endif}}

But I'd like to write them as an array instead. I can only really learn from context so this would be useful as well as neater.

1) How would I write the above as an array?

2) Is it possible to rewrite a call to a key to include a variable name (ie $prefix_definitions)? I haven't been able to do it. If so, how?

 

Link to comment
Share on other sites

That's great -- thank you!

It's leading me to another question. My conditional statements for this take the form:

{{if $record->field_x}}

where x is:

{{$difinoj = array(1 => 5, 7 => 27, 8 => 28);}}
{{$difino = $difinoj[$record::database()->id];}}

How do I rewrite

{{if $record->field_x}}

along the same principles as 

{$record->customFieldDisplayByKey($prefix . '_definitions', 'display')|raw}

earlier?

I've tried things like

{{if $record->field_$difino}}
{{if $record->'field_' . $difino}}

and a few other but the templates won't save because of errors in the HTML.

I'm particulary confused because

{{if $record->$prefikso . '_difinoj'}}

does work, although unfortunately

{{if $record->'field_key'}}

sometimes gives me false positives so I use the field_x approach now. (I've just checked, actually. In this template, the approach {{if $record->customFieldDisplayByKey($prefikso . '_difinoj')}} is producing a false positive whilst {{if $record->field_28}} is correctly evaluating to FALSE.)

Link to comment
Share on other sites

Another conundrum on the same theme!

Some of my articles have record images; some don't. If they don't have one, I'd like them to have a default image.

This part of my template is working for me currently:

{{if $record->record_image}}background-image:url('{file='$record->record_image' extension='cms_Records'}');{{else}}background-image:url('{media="6"}');{{endif}}

However, I'd like different databases using the same template set to have different default images. I need to change the number in {media="6"} depending on database.

I've used the information above to create a variable $fallback which will give the number required. Unfortunately, {"$fallback"} isn't parsing to the required URL and is staying as text in the HTML:

Untitled-2.jpg.416ce05a21c95b0e8adf2b9d124c536d.jpg

So I've tried to do a rewrite, say:

{{$fallbacks = array(1 => 11, 5 => 6,7 => 11, 8 => 11);}}
{{$fallbackId = $fallbacks[$record::database()->id];}}
{{$fallback = 'media="' . $fallbackId . '"';}}

And unfortunately, however I try to write $fallback is  unsuccessful.

I think I could fix the problem by using a conditional statement to add a distinctive class and let the CSS store the details about the background but I've got one of those brains that wants to find out where I've gone wrong with the current approach first!

Link to comment
Share on other sites

Oh, I'm getting an error. I think I've spotted the problem:

{{$fallbackId = $fallbacks[$record::database()->id];}}

This is a block template, which doesn't have $record as a variable. The previous templates did and so this approach worked.

I need to define $record, don't I? 

I can get this to work if I move the variables within the foreach loop which defines $record like this:

{{foreach $records as $record}}
	{{$fallbacks = array(1 => 11, 5 => 6,7 => 11, 8 => 11);}}
	{{$fallbackId = $fallbacks[$record::database()->id];}}
	{{$fallback = \IPS\cms\extensions\core\OutputPlugins\Media::runPlugin($fallbackId);}}

That seems very inefficient to me so although I've now got a working page, it would be interesting to me to try to improve it. How would I define $record/rewrite $fallbackId?

Link to comment
Share on other sites

If you on the database page you can use

{{$fallbacks = array(1 => 11, 5 => 6,7 => 11, 8 => 11);}}
{{$fallbackId = $fallbacks[\IPS\cms\Databases\Dispatcher::i()->databaseId];}}
{{foreach $records as $record}}
	{{$fallback = \IPS\cms\extensions\core\OutputPlugins\Media::runPlugin($fallbackId);}}

 

Link to comment
Share on other sites

Unfortunately, this isn't a database page. It's a block template for a record feed.

My home page contains latest records from two different databases. Each uses a record feed which uses the same block template. Both of the databases have different fallback images to be used if the user hasn't uploaded their own:

Untitled-3.thumb.jpg.797217da62227464ccf0e6b6dba7b3eb.jpg

One solution would be to keep things as they are now, with the variables set each time the loop runs. The second would be to make a separate identical template but with a different fallback image. Both of those work although I like the idea of solving the problem programatically and efficiently so would be prefer to find another solution if possible 🙂 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...