dschreiber Posted September 19, 2017 Share Posted September 19, 2017 Hello! I'm having some trouble getting a block to work in a template. This is my first attempt, so it's probably something wrong that I'm doing. Here's my template where the block should go: Here's the block Details: And finally, here's the block content: The block I created is a Custom Block / Manual HTML. The block content was originally a chunk of the template content. I was experimenting to see if I could move it into a block with it still able to work. Nope. Instead, the block content simply isn't displayed. Any suggestions? Thanks! Link to comment Share on other sites More sharing options...
opentype Posted September 19, 2017 Share Posted September 19, 2017 11 minutes ago, dschreiber said: I was experimenting to see if I could move it into a block with it still able to work. Nope. “Nope” is correct. These type of blocks are processed independently. They won’t have access to the $database variable just because the blocks are called within the template. Link to comment Share on other sites More sharing options...
dschreiber Posted September 19, 2017 Author Share Posted September 19, 2017 Thanks for the reply. I totally appreciate it. I have a follow-up question: Is it possible to fetch the current database in a block? For example, I tried adjusting my block like this... {{$database = \IPS\cms\Databases::load($database->id);}} <div class="cDevHeader" id="elDevDocs_header" style="background-image:url('/forums/uploads/set_resources_3/6554b6be8c0d829a8bf63ae0c82cf121_codebackground.jpg');"> <h1 class="ipsType_pageTitle"> {$database->_title} <div class='ipsType_normal'> {$database->_description} </div> </h1> </div> .. but got blank output. (Is there some log file I can view that shows me why a block might not display?) Thanks again! Link to comment Share on other sites More sharing options...
opentype Posted September 19, 2017 Share Posted September 19, 2017 Same problem. You still try to use $database. It probably doesn’t show anything at all, because you only output empty variables. Link to comment Share on other sites More sharing options...
dschreiber Posted September 19, 2017 Author Share Posted September 19, 2017 Oh, good point. Is there a way to make my block code aware of the current database? If you see what I'm trying to achieve, can you point me toward a solution? It would be much appreciated. Thanks! Link to comment Share on other sites More sharing options...
opentype Posted September 19, 2017 Share Posted September 19, 2017 Actually, I don’t see why you want to move that piece into a block. Keep it where the variables are. Link to comment Share on other sites More sharing options...
dschreiber Posted September 19, 2017 Author Share Posted September 19, 2017 I'm using that specific piece as a proof-of-concept. I agree that it would be silly to move that particular piece into a block. But in theory, is there a way to make a block aware of the current database? Link to comment Share on other sites More sharing options...
opentype Posted September 19, 2017 Share Posted September 19, 2017 Not these type of blocks unfortunately. Widgets from apps/plugins can do it. Link to comment Share on other sites More sharing options...
dschreiber Posted September 19, 2017 Author Share Posted September 19, 2017 Cool, thanks for the insights. I really appreciate your help. Link to comment Share on other sites More sharing options...
Daniel F Posted September 19, 2017 Share Posted September 19, 2017 You should be able to access the current database via \IPS\cms\Databases\Dispatcher::i()->databaseId Link to comment Share on other sites More sharing options...
dschreiber Posted September 19, 2017 Author Share Posted September 19, 2017 Thanks Daniel. I'll give that a shot. Is that documented anywhere? Link to comment Share on other sites More sharing options...
dschreiber Posted September 19, 2017 Author Share Posted September 19, 2017 Drat, that didn't work either. Here's my modified block code (not working): {{$database = \IPS\cms\Databases\Dispatcher::i()->databaseId;}} <div class="cDevHeader" id="elDevDocs_header" style="background-image:url('/forums/uploads/set_resources_3/6554b6be8c0d829a8bf63ae0c82cf121_codebackground.jpg');"> <h1 class="ipsType_pageTitle"> {$database->_title} <div class='ipsType_normal'> {$database->_description} </div> </h1> </div> Any suggestions? Link to comment Share on other sites More sharing options...
Fosters Posted September 19, 2017 Share Posted September 19, 2017 Replace with {{$database = \IPS\cms\Databases::load(\IPS\cms\Databases\Dispatcher::i()->databaseId);}} And keep in mind that this works ONLY if a database is being viewed! You could use something like to have always a database selected {{if \IPS\cms\Databases\Dispatcher::i()->databaseId}} {{$databaseId = \IPS\cms\Databases\Dispatcher::i()->databaseId;}} {{else}} {{$databaseId = 1;}} {{endif}} {{$database = \IPS\cms\Databases::load($databaseId);}} <div class="cDevHeader" id="elDevDocs_header" style="background-image:url('/forums/uploads/set_resources_3/6554b6be8c0d829a8bf63ae0c82cf121_codebackground.jpg');"> <h1 class="ipsType_pageTitle"> {$database->_title} <div class='ipsType_normal'> {$database->_description} </div> </h1> </div> Hope this helps Link to comment Share on other sites More sharing options...
dschreiber Posted September 19, 2017 Author Share Posted September 19, 2017 Thanks @fosters, that worked perfectly. :-) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.