Inline Template Strings in Vue: When to Use Them

📖 2 minutes read

Vue components can define their HTML in multiple ways, but one often-overlooked option is inline template strings using the template property. While Single-File Components (.vue files) are the standard in modern Vue apps, inline templates have legitimate use cases in Laravel projects.

When to Use Inline Templates

1. Simple, self-contained components
For dashboard widgets, modals, or small interactive elements that don’t need build-time compilation.

2. Legacy compatibility
Inline templates work in both Vue 1.x and 2.x without SFC build setup—perfect for gradual migrations.

3. Blade-Vue hybrid pages
When you want Vue reactivity on specific parts of a traditional Blade view without committing to a full SPA architecture.

4. Quick prototypes
Admin dashboard widgets or internal tools where build complexity isn’t justified.

When NOT to Use Them

  • Complex components with lots of markup (becomes unmaintainable)
  • When you have a proper build pipeline (Webpack/Vite) set up
  • Components needing scoped CSS
  • Production SPAs (use .vue files instead)

Example: Dashboard Widget

export default {
    name: 'OrderSummary',
    props: ['orderId', 'apiUrl'],
    data() {
        return {
            order: null,
            loading: true
        };
    },
    template: `
        <div class="order-summary">
            <div v-if="loading">Loading...</div>
            <div v-else-if="order">
                <h3>Order #{{ order.id }}</h3>
                <p>Status: <span :class="'badge-' + order.status">{{ order.status }}</span></p>
                <p>Total: {{ order.total_formatted }}</p>
            </div>
            <div v-else>Order not found</div>
        </div>
    `,
    mounted() {
        this.fetchOrder();
    },
    methods: {
        fetchOrder() {
            fetch(this.apiUrl)
                .then(r => r.json())
                .then(data => {
                    this.order = data;
                    this.loading = false;
                });
        }
    }
};

Pro Tips

Use template literals (backticks) for multi-line templates. This makes the HTML readable with proper indentation.

Keep it focused. If your template exceeds ~30 lines, it’s a sign you should move to a .vue SFC file.

Escape carefully. When embedding inline templates in Blade files, watch out for conflicts between Vue’s {{ }} and Blade’s syntax. Use @{{ }} or the @verbatim directive.

Inline templates aren’t a replacement for proper SFC architecture, but they’re a valuable tool for hybrid Laravel/Vue applications where you need reactivity without the overhead of a full build pipeline.

Daryle De Silva

VP of Technology

11+ years building and scaling web applications. Writing about what I learn in the trenches.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *