简体   繁体   中英

Joomla Component: include/require php in View

this is my first time making a component for Joomla 3. I'm trying to convert a php website to a component.

Almost every view of my php website is built this way:

  • Header
  • Sidebar
  • Navigation bar
  • Actual content
  • Footer
  • Scripts

So basically it looks like this:

A view from php website

<?php require_once('includes/header.php'); ?>
<body>
<div">
    <?php require_once('includes/sidebar.php') ?>
    <div>
         <?php require_once('includes/navbar.php') ?>
    </div>
    <div>
        <!-- Content or something -->
    </div>
    <?php require_once('includes/footer.php') ?>
</div>
<?php require_once('includes/scripts.php') ?>
<script>
</script>
</body>
</html>

But in Joomla this is not possible. If I try to include or require a php file like above it just doesn't work.

View in component looks like this:

com_component
  ../views
    ../tmpl
      default.php
    view.html.php
    ../includes
      header.php
      footer.php
      sidebar.php
      scripts.php
      navbar.php

Default.php is supposed to show a dashboard on the frontend. This is what I'm trying to do in default.php:

Default.php

<?php include_once('../../includes/header.php') ?>
<body>
<?php include_once('../../includes/sidebar.php') ?>
<?php include_once('../../includes/navbar.php') ?>

 <!-- Some content-->

<?php include_once('../../includes/footer.php') ?>

<!-- etc -->

What I've done

  • I've found some JDocument and JHTML functions which can add stylesheets and javascript to the template. But that is not what I'm looking for.

  • I used Joomla's addCustomTag function but it only shows a commented php line in the template.

  • Tried to make a String of it and passed it through a variable.

Questions

  • Is it possible to include php files in a template (default.php)?
  • If not, is there any other way to do it?
  • If yes, is that good practice in Joomla?

Thanks

To include files in the same directory as the given file, you can use:

include __DIR__ . '/../includes/header.php'

If you don't use DIR the current path is related to the main page (index.php in the site root) and not to the layout (default.php).

A more Joomla compatible solution is to put all your "sub-layouts" in the same folder with a conventional naming like this:

com_component/
    views/
        myview/
            view.html.php
            tmpl/
                default.php
                default_header.php
                default_footer.php
                ....

Then you can use in your main layout the following code:

echo $this->loadTemplate('header');
...
echo $this->loadTemplate('footer');

You don't need a component to do any of this. I mean you can do this but it should only be as an intermediary step since Joomla is designed to make all this easier by having APIs to include modules which is basically what all your pieces are.

You'll notice that in the joomla template file there are places where module positions are loaded. These are things like menus, footers, sidebars etc.

Depending on what they are you should really make each of these subsections a module. In some cases you can probably use existing modules already in Joomla or make a "custom html" module if you are just loading some html. In other cases makin a joomla module is really easy, just a few files, and you can put your php code there, really to start you can put the whole thing in the tmpl/default.php for the module or you can split it between the helper and the layout. You'll be much happier in the long run if you take advantage of using a CMS rather than fighting it.

Components are only for the "body" area, what you need to do is make a template and then the modules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM