Kirby and Vue - Getting started

I’m testing to rebuild one of my Kirby sites with Vue to do some filtering. I’m new to Vue but I’ve successfully set up a Vue route.

This is how the footer.php looks like:

<script src="https://unpkg.com/vue@2.5.2/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router@3.0.1/dist/vue-router.js"></script> 

<div id="app">
    <router-view></router-view>
</div>

<script>
var Home = { template: "<div>Home</div>"}
var About = { template: "<div>About</div>"}

var routes = [
    { path: '/', component: Home },
    { path: '/about', component: About },
];

var router = new VueRouter({
    routes: routes,
    base: '/subfolder',
    mode: 'history'
});

var app = new Vue({
    el: '#app',
    router: router
})
</script>

</body>
</html>

So if you get want to try out Vue, this can be a good start, maybe.

Updates

I did some updates in the script above and added mode and base.

You can also use a route to always point at the home page, which can be useful to not end up with 404 errors.

c::set('routes', array(
  array(
    'pattern' => '(.+)',
    'action'  => function() {
        return site()->visit('/');
    }
  )
));

A nice tutorial on Youtube (44 parts).

1 Like

Hello @jenstornell,

Thanks to share your code.
Do you recommend good tutorials to start with Vue and understand its logic?

I just added one a few seconds ago at the bottom of my post. But I’m really new to this as well.

The example code is very much based on this tutorial: https://www.youtube.com/watch?v=Xktwf4yS2g0

Ho sorry, I didn’t see the Youtube link.

Thanks, I will check it out :slight_smile:

Also, the vue.js documentation is a good starting point.

1 Like

Wouhaou, 33 episodes !
I hope it’s as exciting as Game Of Thrones :wink:

Thanks Sonja

Thanks for sharing! However I wouldn’t recommend relying on a frontend library like Vue for sites that are meant to display information (like any company site, blog etc.). Relying on the frontend is only really useful for “web apps” that are run in a rather controlled environment. Using Vue if you don’t have to (like here: backend templates work just fine) is not good regarding progressive enhancement.

But that doesn’t mean that I want to discourage you from playing with Vue. Actually the opposite, but I wanted to post this here as a warning for others that you probably shouldn’t do this “just because you can” and especially not without thinking about the advantages and disadvantages to other technologies first.

BTW: My company site uses frontend magic as well, but as the templates are still generated in the backend, it works totally fine without it. You can take a look at the source code here.

However I wouldn’t recommend relying on a frontend library like Vue for sites that are meant to display information

I agree that most sites don’t need Vue.

Relying on the frontend is only really useful for “web apps”

About my site, in the end it will probably look and function more like an app than a site. It will have filtering, ajax, sorting, view switching, search, value selector and other things. It will be something similar to http://www.pricerunner.se/cl/1/Mobiltelefoner (not my site).

you probably shouldn’t do this “just because you can” and especially not without thinking about the advantages and disadvantages to other technologies first.

When using jQuery or vanilla js for larger applications, it often get quite complicated in the end. The main reason for that, at least for me, is that if we change anyting in the DOM, we also need to change my jQuery/vanilla code. Because of that, I tried Angular a few years ago. The longer I used it, the less I liked it, because it took over the whole site in a way I did not like. It seems like Vue is a bit less overtaking. It’s not that I just grab a random technology and use it.

Maybe Vue will be THE thing for me, but I need to test it much more before I know.

My company site uses frontend magic as well, but as the templates are still generated in the backend, it works totally fine without it.

Yes. I don’t even see why you need javascript on that site at all. The animations? I guess you have some good reason to use js.

But again, my Vue solution is not ment to be used on every site. It’s ment for people who need to build a javascript heavy site (or app if you want). To inspire people that got tired of jQuery or vanilla js, to try something new like Vue.

Thanks for sharing!

Thank you for your kind words. :slight_smile:

But shouldn’t even such a site be accessible (meaning: the information should be there) if the frontend code doesn’t run or doesn’t run properly for some reason? All the filtering and search could happen via the backend as well and can be enhanced with your Vue implementation.

I agree! That’s mainly the reason we like Vue as well.

I don’t mean you specifically. But please keep in mind that other people read the posts in this forum and use them as an inspiration or maybe even as a place they can copy-paste from. As not everyone knows all of this stuff, I think we should all be a bit careful when posting ideas and “best practices”. As I already wrote above, my post is more a warning for others than criticism of your idea.

Yes, mainly the animations. I wouldn’t put that much effort into a client site as the time that can be spent implementing something like this properly (like I did) is definitely not worth the effect (after all the client pays every minute of my time). But as it’s my own site, I put in that extra bit of effort. :slight_smile:
But with stuff like this it’s always a “do it properly or don’t do it at all”. It’s definitely not worth reducing accessibility or reliability of the site just for such a small effect.

And that’s why I proposed to think carefully about each JS enhancement you add. It can definitely be very useful, but if not done properly, it’s easy to break other important aspects of the site.

Alright. :slight_smile:

I’ve now also added it to Kirby Secrets: https://github.com/jenstornell/kirby-secrets/blob/master/docs/vue.md

Because of your concern, I also added this:

Vue is a js framework and if your site is not a js web app, you probably don’t need it.

1 Like

Please don’t tag this as a solution or add it to Kirby Secrets.
People that want to use Vue should learn a lot more about the framework before they use it.
You just happened to follow a tutorial and managed to set up a Vue route, that’s such a little feature and not a real ‘solution’ to a use case yet.
I don’t mean to be rude, but in my opinion, you’re only going to confuse people this way.

1 Like

Not too much love was given here, so I set this topic to “solved” and I removed Vue from the “Table of contents” in Kirby Secrets.

I’m pretty happy with this starting point, thanks for that!

1 Like

I don’t think there is a great use for Vue in the front-end of a Kirby website. Now the Kirby Panel, that’s another story :wink:

Maybe you could opt to not use the vue-router or vuex packages and simply use Vue components to add dynamic elements to already existing Kirby templates. I agree with Lukas that there is no need for Vue to handle the routing or the state for simple websites, but you could for example build an interactive contact form with Vue and then include that in your template :slight_smile:

Doesn’t that depend on what type of site it is? We have no idea of what kind of project @weitweit want to create. If it’s a kind of app like site with tons of features, why not use Vue?

If it’s a site on the web, then Vue is always wrong? I don’t buy that. I think it depends on what kind of site you are making.

Not really, I was only talking about Kirby websites. From my experience people tend to use Kirby to build text/image-heavy websites that deliver a lot of information. If you take a look at the Built with Kirby thread, you’ll see that 90% of the projects in there are portfolio’s, company websites, blogs or galleries. If you’re building an application, you should look elsewhere than Kirby imo. I myself love using Symfony and Slim to build applications and APIs, and Vue is a great fit in that scenario :slight_smile:

Right tool for the job and all that.