Jump to content

{{if}} statement in add attribute


Recommended Posts

I want to add a attribute to a certain class but I only want to add if it the user wants it there, is there any way to add a {{if}} statement to it, I tried adding it just before the attribute but it give a whoops error and if I add a if statement before the class with a endif after it it hides the whole section. I got a feeling the only way to do it is to replace the whole code and add the statement in there that way? does any one know how I can add it without replacing the whole code?

Link to comment
Share on other sites

3 minutes ago, TAMAN said:

Adding attribute to a class? what do you mean? show your code what you trying to do :) 

into a division i mean not a class I want to add a ipsSticky=" " attribute to the nav bar if I add it it's fine but I only want it to be used if the user wants it active

Link to comment
Share on other sites

4 minutes ago, TheJackal84 said:

into a division i mean not a class I want to add a ipsSticky=" " attribute to the nav bar if I add it it's fine but I only want it to be used if the user wants it active

This doesnt work?

<nav {{if $blah == 1}}ipsSticky=""{{endif}}>...</nav>

 

 

Link to comment
Share on other sites

Just now, TAMAN said:

This doesnt work?


<nav {{if $blah == '1'}}ipsSticky=""{{endif}}</nav>

yes but I don't want to replace the whole code I have it working with replacing the code but I would rather find a way to just use the add attribute section of the dev settings with a if statement if that's possible

Link to comment
Share on other sites

1 minute ago, TheJackal84 said:

yes but I don't want to replace the whole code I have it working with replacing the code but I would rather find a way to just use the add attribute section of the dev settings with a if statement if that's possible

You dont replace the whole code with the if statement, I edited my post i wrote too fast

Link to comment
Share on other sites

5 minutes ago, TAMAN said:

You dont replace the whole code with the if statement, I edited my code i wrote too fast

 

But everything inside the

<nav {{if $blah == 1}}ipsSticky=""{{endif}}>...</nav>

division will need to be copied and replaced, I basically don't want to use the replace the chosen elements settings I want to use the add attribute one

with custom themes its easier to use the add attributes one then replacing as they might have custom divisions

Link to comment
Share on other sites

Just now, TheJackal84 said:

 

But everything inside the


<nav {{if $blah == 1}}ipsSticky=""{{endif}}>...</nav>

division will need to be copied and replaced, I basically don't want to use the replace the chosen elements settings I want to use the add attribute one

Still not clear what you want to do :/ 

Link to comment
Share on other sites

1 minute ago, TAMAN said:

Nope, Not a bit :D Just paste your code and explain what you trying to do 

Its nothing to do with the code

In the settings there is a way to add attributes to divisions and elements in themes and there is a way to replace the code of themes, I want to add a attribute with a {{if}} statement the only way I found I can do it is by replacing the code but if there is a way to do it using the add attribute setting then thats better for me as some themes use their own divisions and elements and my plugin may not work with it or might mess up the style of it

XSlVRRi.png

but it don't work I want to know if there is a way to use a if statement with it, I can replace the code but I don't want that if there is a way to use the setting above

Link to comment
Share on other sites

EDIT: somehow my post got emptied and only the codebox remained.

1 hour ago, TheJackal84 said:

Its nothing to do with the code

In the settings there is a way to add attributes to divisions and elements in themes and there is a way to replace the code of themes, I want to add a attribute with a {{if}} statement the only way I found I can do it is by replacing the code but if there is a way to do it using the add attribute setting then thats better for me as some themes use their own divisions and elements and my plugin may not work with it or might mess up the style of it

XSlVRRi.png

but it don't work I want to know if there is a way to use a if statement with it, I can replace the code but I don't want that if there is a way to use the setting above

You can't use statements there. I've been asking about adding support for them there since 4.0.

 

You can use the same workaround I used in this plugin I wrote:

 

First I add always a CSS class without caring about the statement (you can do it with attributes too):

TB.HIDDEN.REPLACEMENT.FOR.HIDE.QUOTE.BUTTON.FROM.FIRST.POST

 

Then I overload the post template and use a statement to decide how to replace the text, either with the css class I need or an empty string:

public function post( $item, $comment, $editorName, $app, $type, $class='' )
{
	$output = parent::post( $item, $comment, $editorName, $app, $type, $class );
	
	// Replacement check
	$hiddenReplacement = $comment->isFirst() ? ( ( \IPS\Settings::i()->tb_hqbfp_allow AND in_array( $item->forum_id, explode( ',', \IPS\Settings::i()->tb_hqbfp_allow ) ) ) ? '' : 'ipsHide' ) : '';
	
	// Finally replace it in
	$output = str_replace( 'TB.HIDDEN.REPLACEMENT.FOR.HIDE.QUOTE.BUTTON.FROM.FIRST.POST', $hiddenReplacement, $output );
	
	return $output;
}

 

Link to comment
Share on other sites

8 minutes ago, teraßyte said:

EDIT: somehow my post got emptied and only the codebox remained.

You can't use statements there. I've been asking about adding support for them there since 4.0.

 

You can use the same workaround I used in this plugin I wrote:

 

First I add always a CSS class without caring about the statement (you can do it with attributes too):


TB.HIDDEN.REPLACEMENT.FOR.HIDE.QUOTE.BUTTON.FROM.FIRST.POST

 

Then I overload the post template and use a statement to decide how to replace the text, either with the css class I need or an empty string:


public function post( $item, $comment, $editorName, $app, $type, $class='' )
{
	$output = parent::post( $item, $comment, $editorName, $app, $type, $class );
	
	// Replacement check
	$hiddenReplacement = $comment->isFirst() ? ( ( \IPS\Settings::i()->tb_hqbfp_allow AND in_array( $item->forum_id, explode( ',', \IPS\Settings::i()->tb_hqbfp_allow ) ) ) ? '' : 'ipsHide' ) : '';
	
	// Finally replace it in
	$output = str_replace( 'TB.HIDDEN.REPLACEMENT.FOR.HIDE.QUOTE.BUTTON.FROM.FIRST.POST', $hiddenReplacement, $output );
	
	return $output;
}

 

Cheers I will give it a play with and see what I come up

1 minute ago, HeadStand said:

Been asking for the same thing since 4.0 as well. :(

It would make it so much easier if they allowed it in some form or way

Link to comment
Share on other sites

  • 2 weeks later...

I did manage to get the code implanted into the template by adding the code below into the template

{{$test = 'the_codes_value';}}
{{$test1 = '{{if \IPS\Settings::i()->My_setting == 1}}';}}
{{$test2 = '{{endif}}';}}

And then as the setting it was

Key = whatever the key is

and the value was below

{$test1|raw}{$test}{$test2|raw}

it did show into the template perfect but it didn't implement it (work) it was just there showing like

{{if \IPS\Settings::i()->My_setting == 1}}the_codes_value{{endif}}

Maybe someone could work from this and get a way for it to work? Unless we nag IPS until they add it so we can use a {{if}} statement in there.

Link to comment
Share on other sites

Ok I done it and got it to work basically you need to add this sort of code to the template

{{if \IPS\Settings::i()->My_setting == 0}}
    {{$test = 0;}}
{{else}}
    {{$test = 1;}}
{{endif}}

and as the attribute the key will be what ever your key is and the value will be

{$test}

then it will show if your setting is selected

 

@HeadStand

Link to comment
Share on other sites

11 minutes ago, TheJackal84 said:

Ok I done it and got it to work basically you need to add this sort of code to the template


{{if \IPS\Settings::i()->My_setting == 0}}
    {{$test = 0;}}
{{else}}
    {{$test = 1;}}
{{endif}}

and as the attribute the key will be what ever your key is and the value will be


{$test}

then it will show if your setting is selected

 

@HeadStand

Sorry, I wouldn't do it this way. Editing a template isn't a solution for development purposes.

The only way that I know of to do this (via plugin) is by inserting a hidden div anywhere in the template (using a theme hook) with the setting value inside. Then using a JS controller to handle the value and add the attributes accordingly.

 

Link to comment
Share on other sites

2 minutes ago, HeadStand said:

Sorry, I wouldn't do it this way. Editing a template isn't a solution for development purposes.

The only way that I know of to do this (via plugin) is by inserting a hidden div anywhere in the template (using a theme hook) with the setting value inside. Then using a JS controller to handle the value and add the attributes accordingly.

 

If your making a plugin or app then just create a hook and add it, what's wrong with that?

Link to comment
Share on other sites

2 minutes ago, TheJackal84 said:

If your making a plugin or app then just create a hook and add it what's wrong with that?

Edit: I see now what you did. You have a global value for the entire template. Yes, that will work.

The use case I was thinking of is when you have multiple attributes in a single template that will change. For example, putting things on the rows of the forum index.

Link to comment
Share on other sites

Just now, HeadStand said:

Because a template edit means that your template is now stuck that way and won't be updated with the next version's changes. That means that I'd have to remember to check all templates for differences and incorporate them. There's too much room for error.

The JS controller will work even when you update to the next version.

I don't get how you mean, If you wanted to add a sticky nav bar for example then just create a hook in global and add it to global template or the navbar template at the top, it don't need updating, If you use it on multi pages then place it under the </head> or something in global, its only a {{if}} statement asking 0 or 1

Link to comment
Share on other sites

3 minutes ago, TheJackal84 said:

I don't get how you mean, If you wanted to add a sticky nav bar for example then just create a hook in global and add it to global template or the navbar template at the top, it don't need updating, If you use it on multi pages then place it under the </head> or something in global, its only a {{if}} statement asking 0 or 1

See my edited post. ;)

I misunderstood what you were  doing.

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...