Velocity布局模板创建失败求助:默认HTML模板无法正常工作
Looking at your code, there are a couple of key issues that are likely preventing your Velocity layout from working correctly. Let's break them down and fix them step by step:
1. Missing Macro Import
Your menu.vm is trying to use the mainLayout macro defined in main.vm, but Velocity doesn’t automatically load other template files by default. You need to explicitly parse the macro file at the top of menu.vm so the engine recognizes the mainLayout macro.
Fix for menu.vm:
Add #parse at the start of the file to import the main layout macro. Make sure the path matches where your main.vm is stored (assuming both are in the views directory):
#parse("views/main.vm") #@mainLayout() <nav class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">Kicker</a> </div> <div id="navbar" class="collapse navbar-collapse"> <ul class="nav navbar-nav"> <li class="active"><a href="/">Home</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Player <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="/player/list">Players list</a></li> <li><a href="/player/new">New Player</a></li> <li role="separator" class="divider"></li> </ul> </li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Match <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="/match/list">Match list</a></li> <li><a href="/match/new">New Match</a></li> <li role="separator" class="divider"></li> </ul> </li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Season <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="/season/list">Season list</a></li> <li><a href="/season/new">New Season</a></li> <li role="separator" class="divider"></li> </ul> </li> <li><a href="/match/dashboard">Dashboard</a></li> <li><a href="/stats">Statistics</a></li> </ul> </div> </div> </nav> #end
2. Verify Template Path Configuration
Ensure your Velocity engine is configured to look for templates in the correct directory. If your main.vm is in the same views folder as menu.vm, the #parse("views/main.vm") path should work. If main.vm is directly in the root template directory, you can simplify it to #parse("main.vm").
3. Confirm Block Macro Syntax
Your use of Velocity's block macro syntax (#@mainLayout() ... #end) is correct, and the $!bodyContent variable in main.vm will correctly render the content inside the block. Just double-check there are no typos in the variable name (it’s case-sensitive!).
Additional Checks
- Restart your application after making these changes to ensure the template engine picks up the updated files.
- Check your application logs for any Velocity-specific errors (like "macro not found" or template path issues) which can give you more clues if the problem persists.
内容的提问来源于stack exchange,提问作者Max Günther

