How to make nav bar common for all pages in thymeleaf - spring-thymeleaf

I am using thymeleaf for my front end design ,I create a nav bar and
I want this nav bar work for all pages, my Requirement is make one nav
bar and use this nav bar in every page ,with out define another nav
bar in separate page .
I create a nav bar inside my fragments folder that situated inside
template folder
When i call my nav bar in my index page it is not working
Here is my code of index page
index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Coursel</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" ></script>
<script type="text/javascript" src="/static/demo.js"></script>
</head>
<body>
<nav th:replace="/fragments/nav :: nav-front"></nav>
<div class="container-fluid">
here my register form codes
</div>
</body>
</html>
nav.html
<nav class="nav">
<a class="nav-link active" th:href="#{/templates/index.html}">Active</a>
<a class="nav-link active" th:href="#{/templates/index.html}">Register</a>
<a class="nav-link active" th:href="#{/templates/index.html}">Login</a>
<a class="nav-link active" th:href="#{/templates/index.html}">AboutUs</a>
</nav>
Here is my project structure
I use IntelliJ IDEA as code editor.

It is possible, look into Thymeleaf Layout Dialect.
In short:
you create some layout.html in your templates folder. In it:
<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<!-- all common head tags - meta etc. -->
<title layout:title-pattern="$CONTENT_TITLE - $LAYOUT_TITLE">App name</title><!-- define title in your page template and it will be added too your app name, e.g. 'Home - My App' -->
</head>
<body>
<nav>
<!-- your nav here -->
</nav>
<section layout:fragment="content">
<!-- don't put anything here, it will be replaced by the page contents -->
</section>
</body>
</html>
And in you page templates:
<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{/path/to/layout.html}">
<head>
<title>Page name</title>
</head>
<body>
<section layout:fragment="content">
<p>This will be added into your layout!</p>
</section>
</body>
</html>
In order to make it work, you will need to add it to your templateEngine() bean definition:
#Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver( thymeleafTemplateResolver() );
templateEngine.addDialect( new LayoutDialect() );
return templateEngine;
}
See mvnrepository to import this dialect using maven.

Related

Tailwind Flex - Width full size

index.html:
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<link rel="icon" href="<%= BASE_URL %>favicon.ico" />
<title><%= htmlWebpackPlugin.options.title %></title>
</head>
<body>
<noscript>
<strong
>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work
properly without JavaScript enabled. Please enable it to
continue.</strong
>
</noscript>
<div id="app" class="bg-red-300"></div>
<!-- built files will be auto injected -->
</body>
</html>
App.vue:
<template>
<div class="flex flex-col md:flex-row">
<Nav class="bg-gray-300 md:w-1/4" />
<router-view class="bg-yellow-400" />
</div>
</template>
<script>
import Nav from './components/nav_components/Nav.vue';
export default {
name: 'App',
components: {
Nav,
},
};
</script>
<style></style>
640px Tailwind's small breakpoint (sm)
768px Tailwind's medium breakpoint (md)
Until 768px I use flex-col, and for bigger screen sizes I use flex-row as you can see in my code as well.
RESULT: (0-768px width, flex-col):
Between 640px and 768px the width of both my Nav and router-view containers stop expanding and I can see the red background of my where I mount my App.
RESULT: (>768px width, flex-row):
After 2050px, my container stops expanding which leads to the same result as mentioned before.
What can i do to workaround this behaviour?
Max width was by default 1536px. Setting max-w-full to my router-view container fixed the problem.

Nodejs Handlebars Dynamic Partials use

I have a main hbs, which is the main layer of my layouts. In this hbs I want to set one partial hbs based on if a person is logged in or not. If he's logged in I want to show a userInfo partial if he's not I want to see the >navbar partial for example. The main problem is, that if I render again this main giving a value to the userInfo partial (and checkig if the userInfo #eq value) the main page is rendered twice. How can I set and switch from between these two partials properly based on some value? Thank you very much.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{{ title }}</title>
<link href="/style.css"
rel="stylesheet"
type="text/css">
</head>
<body>
{{> userInfo}}
{{> nav}}
{{{ body }}}
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{{ title }}</title>
<link href="/style.css"
rel="stylesheet"
type="text/css">
</head>
<body>
{{#if userInfo}}
{{> userInfo}}
{{else}}
{{> nav}}
{{/if}}
{{{ body }}}
</body>
</html>
Can you try this one? I think this will help you. You can also add this condition to server side to make this decision.

route not working in angularjs

I am using angular along with node.
I am using routes to redirect page.
Onto my html page i have defined 2 links as:
<!DOCTYPE html>
<html >
<head>
<title>My Angular App!</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js"></script>
<script src="../public/javascript/angular-route.min.js"></script>
<link data-require="bootstrap-css#*" data-semver="3.2.0" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
<script src="/webstorm1/app.js"></script>
<script src="../controllers/registration.js"></script>
</head>
<body>
<div ng-app="test">
<a ng-href="#/login">Login</a>
<a ng-href="#/register">Register</a>
</div>
<div ng-view></div>
</body>
</html>
and onto the app.js file i have defined the route as:
var test = angular.module('test', ['ngRoute'])
.config(function($routeProvider, $locationProvider) {
console.log('test');
$routeProvider.when('/register',
{
templateUrl: '/webstorm1/partials/register.html',
controller: 'registration'
});
$routeProvider.when('/login',
{
templateUrl: '../partials/login.html',
controller: 'login'
});
});
when i run the html file and click on register link, it prints the 'test', but doesnot show the page on browser.
Your <div ng-view></div> is outside of <div ng-app="test"> scope.
<div ng-app="test"> <!-- ng-app starts -->
<a ng-href="#/login">Login</a>
<a ng-href="#/register">Register</a>
</div> <!-- ng-app ends -->
<div ng-view></div> <!-- outside from ng-app -->
Simply put that ng-app="test" to some parent element like body or html to make all elements inside their scope.

jQuery slider not working in angularjs

My slider wont come up on first time navigation of the page. However when I hard refresh the page then it comes up and also if I replace the <div ng-view></div> code with the front.html page content then also the slider starts working. Let me know what I am doing wrong in angular as I believe it is something basic that I am missing.
Following is my layout code -
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta charset="utf-8" />
<title>WEBSITE</title>
<link rel="stylesheet" href="assets/css/style.css" />
<link rel="stylesheet" href="assets/css/flexslider.css" type="text/css" media="screen" />
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="assets/js/jquery.js"></script>
<script src="assets/js/jquery.flexslider.js"></script>
<script src="assets/js/angular.js"></script>
<script src="assets/js/angular-route.js"></script>
<script src="controllers/mainController.js"></script>
<script>
jQuery(document).ready(function($) {
jQuery(window).load(function() {
jQuery('.flexslider').flexslider({
animation: "slide"
});
});
});
</script>
</head>
<body ng-controller="mainController">
<header ng-include="'includes/header.html'"></header>
<nav ng-include="'includes/navmenu.html'"></nav>
<div ng-view></div>
<footer ng-include="'includes/footer.html'"></footer>
</body>
</html>
Following is my main controller code -
var myApp = angular.module('myApp', ['ngRoute']);
myApp.config(function($routeProvider){
$routeProvider
.when('/', {
templateUrl: 'views/pages/front.html',
controller: 'frontCtrl'
})
});
In front.html I have slider code -
<div class="flexslider">
<ul class="slides">
<li>
<img src="assets/images/banner1.jpg">
</li>
<li>
<img src="assets/images/banner2.jpg">
</li>
<li>
<img src="assets/images/banner3.jpg">
</li>
<li>
<img src="assets/images/banner4.jpg">
</li>
</ul>
</div>
EDIT -
If I also write this code in the Firebug console then also slider starts working but not on page load -
jQuery('.flexslider').flexslider();
The event handler attached with jQuery(window).load will run when the page is fully loaded. However, ng-view hasn't loaded your view yet, which means <div class="flexslider"> doesn't exist.
Move the initialization into a directive:
myApp.directive('flexslider', function () {
return {
link: function (scope, element, attrs) {
element.flexslider({
animation: "slide"
});
}
}
});
And use like this:
<div class="flexslider" flexslider>
Demo: http://plnkr.co/edit/aVC9fnRhMkKw3xfpm4No?p=preview

How to prevent layout rendering when following a link

I have a view that should not be using a layout.
This is the controller action used :
def last
#video = Video.last
render layout: false
end
When I call the "show" view directly, it works, no layout are used.
Nonetheless, when I call that view by following a link then it actually loads the default layout with my view.
the link is present in a layout :
<%= link_to "last video", last_video_path %>
and the view :
<body marginwidth="0" marginheight="0">
<iframe type="text/html" width="100%" height="100%" frameborder="0"
src="http://www.youtube.com/embed/<%= #video %>" >
</iframe>
</body>
Any idea what I might be doing wrong ??
I finally found what was not working. In my layout, I have in the header :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= content_for?(:title) ? yield(:title) : "NBATopOfTheNight" %></title>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
</head>
<body>
...
the line <%= javascript_include_tag "application" %> is added by twitter bootstrap and is responsible for rendering my views even when I specify no rendering.
Deleting this line make my project works :-)

Resources