# Can't get v-if to work in a custom block in the Panel

**URL:** <https://forum.getkirby.com/t/cant-get-v-if-to-work-in-a-custom-block-in-the-panel/30839>\
**Category:** Questions\
**Tags:** v4\
**Created:** [February 19, 2024, 9:08pm UTC](https://forum.getkirby.com/t/cant-get-v-if-to-work-in-a-custom-block-in-the-panel/30839 "2024-02-19T21:08:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![whitmoa](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/whitmoa/32/16395_2.png) [@whitmoa](https://forum.getkirby.com/u/whitmoa)\
**Post date:** [February 19, 2024, 9:08pm UTC](https://forum.getkirby.com/t/cant-get-v-if-to-work-in-a-custom-block-in-the-panel/30839/1 "2024-02-19T21:08:29Z")

</div>

I’m creating a custom button block. The blueprint and snippet work perfectly in that I can enter the relevant fields and the button appears as intended in the front-end.

One of the blueprint fields is a `toggles` field for selecting alignment, with the chosen option adding an appropriate class. Although it works perfectly in the front-end, I want to be able to align the button in the Panel. For that, I’m trying to use a v-if conditional statement in the index.js file, But I can’t get it to work.

My index.js file looks like this…

```auto
panel.plugin("brandsistency/button-block", {
  blocks: {
    button: {
      template: `
        <button v-if="content.alignment === 'center'" class="txt-c" type="button" @click="open">{{ content.text }}</button>
        <button v-else-if="content.alignment === 'right'" class="txt-r" type="button" @click="open">{{ content.text }}</button>
        <button v-else type="button" @click="open">{{ content.text }}</button>
      `
    }
  }
});

```

The relevant section of the blueprint file looks like this…

```auto
alignment:
  type: toggles
  width: 1/2
  labels: false
  grow: false
  options:
    - value: center
      icon: text-center
    - value: right
      icon: text-right

```

If I choose center alignment and look in the page’s .txt file, there is a content\>alignment\>center sequence and the txt-c class does appear (from the snippet) in the button in the front-end. But if I look in the Panel, the button is there but the txt-c class is not added.

**What is wrong with my index.js file?** How do I get the relevant classes to add to the button in the Panel? I followed the [guidance in the docs](https://getkirby.com/docs/guide/page-builder/custom-block-type) but I’m obviously missing something.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 19, 2024, 9:17pm UTC](https://forum.getkirby.com/t/cant-get-v-if-to-work-in-a-custom-block-in-the-panel/30839/2 "2024-02-19T21:17:16Z")

</div>

Frankly, I don’t think this is the right way to go about it. The only thing that differentiates your three buttons is the class you add to it (or don’t add to it).

So ideally, you have only one button element in your template, then use a method that returns the class name, and add that value to the class attribute.

---

<div class="post-metadata">

**Author:** ![whitmoa](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/whitmoa/32/16395_2.png) [@whitmoa](https://forum.getkirby.com/u/whitmoa)\
**Post date:** [February 19, 2024, 10:32pm UTC](https://forum.getkirby.com/t/cant-get-v-if-to-work-in-a-custom-block-in-the-panel/30839/3 "2024-02-19T22:32:10Z")

</div>

Yes, that would be ideal, Sonja. And that’s the approach taken in the snippet – a single element with PHP if statements to add the relevant class. But I haven’t be able to work out how to make it work in the JS file for the Panel.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 19, 2024, 10:58pm UTC](https://forum.getkirby.com/t/cant-get-v-if-to-work-in-a-custom-block-in-the-panel/30839/4 "2024-02-19T22:58:05Z")

</div>

Not very much different in JS:

```auto
panel.plugin("brandsistency/button-block", {
  blocks: {
    button: {
      template: `
        <button :class="className" type="button" @click="open">{{ content.text }}</button>
      `,
      computed: {
        className() {
          let value = this.content.alignment;
          let className = value === 'center' ? 'txt-c' : value === 'right' ? 'txt-r' : '';
          return className;
        }
      },
    }
  }
});

```

BTW: We have quite a few examples in the guide: [Page builder | Kirby CMS](https://getkirby.com/docs/guide/page-builder)

---

<div class="post-metadata">

**Author:** ![whitmoa](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/whitmoa/32/16395_2.png) [@whitmoa](https://forum.getkirby.com/u/whitmoa)\
**Post date:** [February 20, 2024, 9:03pm UTC](https://forum.getkirby.com/t/cant-get-v-if-to-work-in-a-custom-block-in-the-panel/30839/5 "2024-02-20T21:03:50Z")

</div>

Thanks, Sonja. That looks really promising. But on trying it, I think the issue I’m having is that my button block isn’t updating at all.

My index.js now looks very similar to your suggestion (I added a placeholder) but there is no change in the Panel. Even if I hardcode `class="txt-c"` into the `<button>` tag, when I look at Dev Tools in the Panel, all I see is the original…

`<button type="button">text</button>`

I updated the plugin version number in the composer.json file and that updated number shows up in the Panel’s System page. I’ve tried clearing cache, media folders, etc. but without success. It’s probably something obvious but I can’t figure out why the `<button>` tag isn’t updating in the Panel. Do I need to do something to force the plugin to use the latest version of index.js?

This is my latest index.js…

```auto
panel.plugin("brandsistency/button-block", {
  blocks: {
    button: {
      computed: {
        placeholder() {
          return "Button text...";
        },
        className() {
          let value = this.content.alignment;
          let className = value === 'center' ? 'txt-c' : value === 'right' ? 'txt-r' : ''; return className;
        }
      },
      template: `
        <button :class="className" type="button" @click="open">{{ content.text }}</button>
      `
    }
  }
});

```

---

<div class="post-metadata">

**Author:** ![whitmoa](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/whitmoa/32/16395_2.png) [@whitmoa](https://forum.getkirby.com/u/whitmoa)\
**Post date:** [February 21, 2024, 12:52am UTC](https://forum.getkirby.com/t/cant-get-v-if-to-work-in-a-custom-block-in-the-panel/30839/6 "2024-02-21T00:52:37Z")

</div>

For further clarification…

- I have also created custom plugins to add center and right alignment buttons to the Writer field editing bar. They work perfectly in that the snippet adds “txt-c” and “txt-r” in the front-end **and** the index.js file also adds the classes in the Panel so they can be targeted via panel.css to align the text preview.

- However, with this button-block plugin, the snippet successfully adds the alignment classes in the front-end **but** , no matter what changes I make to the index.js file, nothing happens to the button preview in the Panel.

What am I getting wrong? **Why are changes to index.js having no effect?**

---

<div class="post-metadata">

**Author:** ![whitmoa](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/whitmoa/32/16395_2.png) [@whitmoa](https://forum.getkirby.com/u/whitmoa)\
**Post date:** [March 10, 2024, 11:35am UTC](https://forum.getkirby.com/t/cant-get-v-if-to-work-in-a-custom-block-in-the-panel/30839/7 "2024-03-10T11:35:13Z")

</div>

Not sure what the (caching?) issue was but I’ve (a) just got a new laptop and (b) rebuilt the button-block from scratch and my problem has been solved.
